尝试规范化和推送时出错 "Assertion Failed: Expected an object as `data` in a call to `push`/`update`"
Error "Assertion Failed: Expected an object as `data` in a call to `push`/`update`" when trying to normalize and push
在我的 Ember 应用程序中,我收到来自网络套接字的通知。当websocket收到一条消息(编码为JSON),我想把它推入store.
到目前为止,这是我的代码:
console.log('About to normalize', data);
var normalizedData = this.store.normalize('message', data);
console.log('About to push', normalizedData);
this.store.push('message', normalizedData);
data 和 normalizedData 最终是完全相同的值,就像这样:
{"message":{"id":1,"chatroom":1,"player":1,"content":"A message"}}
并且调用 push 方法引发了这个错误:
Error: Assertion Failed: Expected an object as `data` in a call to `push`/`update` for message ,
but was {"message":{"id":1,"chatroom":1,"player":1,"content":"31232132113"}}
我不知道怎么了。当Ember 从服务器得到一个特定的消息时,它接收到相同类型的JSON 和Ember 数据处理得很好。当它来自 websocket 并需要推送时,它会崩溃。
更新
我尝试使用 pushPayload,而不是像评论中建议的那样。它仍然不起作用。我收到这些消息:
"WARNING: Encountered "0" in payload, but no model was found for model name "0" (resolved model name using arkipel@serializer:-rest:.typeForRoot("0"))"
"WARNING: Encountered "0" in payload, but no model was found for model name "1" (resolved model name using arkipel@serializer:-rest:.typeForRoot("1"))"
"WARNING: Encountered "0" in payload, but no model was found for model name "2" (resolved model name using arkipel@serializer:-rest:.typeForRoot("2"))"
它上升到第 67 位,然后继续使用 fmt、camelize、htmlSafe 等词...我很漂亮,只是数据只是一个代表 JSON.
的字符串
我们在聊天中进行了一些调试后发现了问题。我会在这里为其他人分享解决方案。
websocket 正在以字符串格式发送消息。所以 javascript 代码中的 data
是一个字符串而不是 javascript 对象 (JSON)。 Ember 数据希望我们推送 javascript 对象而不是字符串。
解决方案是先将来自 websocket 的响应解析为 javascript 对象,然后再将其推送到商店中,例如通过执行 JSON.parse(data)
.
在我的 Ember 应用程序中,我收到来自网络套接字的通知。当websocket收到一条消息(编码为JSON),我想把它推入store.
到目前为止,这是我的代码:
console.log('About to normalize', data);
var normalizedData = this.store.normalize('message', data);
console.log('About to push', normalizedData);
this.store.push('message', normalizedData);
data 和 normalizedData 最终是完全相同的值,就像这样:
{"message":{"id":1,"chatroom":1,"player":1,"content":"A message"}}
并且调用 push 方法引发了这个错误:
Error: Assertion Failed: Expected an object as `data` in a call to `push`/`update` for message ,
but was {"message":{"id":1,"chatroom":1,"player":1,"content":"31232132113"}}
我不知道怎么了。当Ember 从服务器得到一个特定的消息时,它接收到相同类型的JSON 和Ember 数据处理得很好。当它来自 websocket 并需要推送时,它会崩溃。
更新
我尝试使用 pushPayload,而不是像评论中建议的那样。它仍然不起作用。我收到这些消息:
"WARNING: Encountered "0" in payload, but no model was found for model name "0" (resolved model name using arkipel@serializer:-rest:.typeForRoot("0"))"
"WARNING: Encountered "0" in payload, but no model was found for model name "1" (resolved model name using arkipel@serializer:-rest:.typeForRoot("1"))"
"WARNING: Encountered "0" in payload, but no model was found for model name "2" (resolved model name using arkipel@serializer:-rest:.typeForRoot("2"))"
它上升到第 67 位,然后继续使用 fmt、camelize、htmlSafe 等词...我很漂亮,只是数据只是一个代表 JSON.
的字符串我们在聊天中进行了一些调试后发现了问题。我会在这里为其他人分享解决方案。
websocket 正在以字符串格式发送消息。所以 javascript 代码中的 data
是一个字符串而不是 javascript 对象 (JSON)。 Ember 数据希望我们推送 javascript 对象而不是字符串。
解决方案是先将来自 websocket 的响应解析为 javascript 对象,然后再将其推送到商店中,例如通过执行 JSON.parse(data)
.