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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With