未捕获的TypeError:无法将Symbol值转换为字符串 [英] Uncaught TypeError: Cannot convert a Symbol value to a string
问题描述
我从服务器收到以下JSON:
I'm receiving the following JSON from the server:
然后我试图在AJAX调用的 success
中使用$ .map映射它,如下所示:
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:无法将Symbol值转换为字符串
Uncaught TypeError: Cannot convert a Symbol value to a string
尝试映射时具有符号
名称的属性。
when it tries to map the property with the Symbol
name.
从我读过的,javascript最近(或至少计划添加新的符号原始类型。这个问题可能有关系吗?有什么解决方法?任何帮助都非常感激。
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.
推荐答案
这里的问题是KO正在尝试使用一个名为的函数符号
(因为KO observables是函数),因为数据中的一个属性称为 Symbol
。但是在ES2015引擎上,将会有一个全局 Symbol
函数作为JavaScript环境的一部分。所以KO调用该函数,得到一个 Symbol
而不是它所期望的,然后(显然)做了一些试图将该值强制转换为字符串的操作。哪个失败了。 (我不确定为什么它最终调用全局符号
而不是影响它的东西,但是KO使用了一些相当复杂的动态代码并且带
语句,所以...)
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...)
这将是最新JavaScript语言带来的KO映射器中的错误变化。作为短期修复,请在映射之前重命名该属性。
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.
这篇关于未捕获的TypeError:无法将Symbol值转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!