Uncaught TypeError: Cannot convert a Symbol value to a string

*爱你&永不变心* 提交于 2019-12-23 09:29:12

问题


I'm receiving the following JSON from the server:

And then I'm trying to map it using $.map in the AJAX call's success, as follows:

$.ajax({
        type: "GET",
        url: urlGetStaticData,
        success: function (data) {
            self.AvailableTags(data[0].Value);
            self.MeasurementUnits($.map(data[1].Value, function (item) { return ko.mapping.fromJS(item) }));

and the last line throws the following exception:

Uncaught TypeError: Cannot convert a Symbol value to a string

when it tries to map the property with the Symbol name.

From what I've read, javascript has recently (or atleast had planned to) added a "new Symbol primitive type". Could this issue be related? What workaround is there? Any help greatly appreciated.


回答1:


The problem here is that KO is trying to use a function called Symbol (because KO observables are functions) because one of the properties in your data is called Symbol. But on an ES2015 engine, there will be a global Symbol function as part of the JavaScript environment. So KO calls that function instead, gets a Symbol back instead of what it's expecting, and then (apparently) does some operation that attempts to coerce that value to a string. Which fails. (I'm not sure why it ends up calling the global Symbol rather than something shadowing it, but KO uses some fairly complex dynamic code and with statements, so...)

This would be a bug in the KO mapper brought on by recent JavaScript language changes. As a short-term fix, rename the property before mapping it.



来源:https://stackoverflow.com/questions/34996890/uncaught-typeerror-cannot-convert-a-symbol-value-to-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!