Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot convert a Symbol value to a string

I'm receiving the following JSON from the server:

enter image description here

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.

like image 947
iuliu.net Avatar asked Jan 25 '16 16:01

iuliu.net


1 Answers

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.

like image 127
T.J. Crowder Avatar answered Oct 09 '22 23:10

T.J. Crowder