Error "Assertion Failed: expected object as" data "when calling` push` / `update` 'when trying to normalize and click

In my Ember app, I get notifications from websocket. When websocket receives a message (encoded as JSON), I want to push it to the repository.

Here is my code:

console.log('About to normalize', data); var normalizedData = this.store.normalize('message', data); console.log('About to push', normalizedData); this.store.push('message', normalizedData); 

and normalizedData are ultimately the exact same values, something like this:

 {"message":{"id":1,"chatroom":1,"player":1,"content":"A message"}} 

And calling the push method raises this error:

 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"}} 

I do not know what is wrong. When Ember receives a specific message from the server, it receives the same kind of JSON and Ember Data that does it well. When it comes from a website and you need to click it, it will work.

Update

I tried using pushPayload as suggested in the comments. This still does not work. I get these messages:

"WARNING: there is" 0 "in the payload, but the model was not found for model name" 0 "(model name is allowed using arkipel @serializer: -rest: .typeForRoot (" 0 "))"

"WARNING: meets" 0 "in the payload, but the model was not found for model name" 1 "(resolved model name using arkipel @serializer: -rest: .typeForRoot (" 1 "))"

"WARNING: there is" 0 "in the payload, but the model was not found for model name" 2 "(model name is allowed using arkipel @serializer: -rest: .typeForRoot (" 2 "))"

It goes up to number 67, and then continues to go with words like fmt, camelize, htmlSafe ... I'm pretty simple data - it's just a string representing JSON.

+5
source share
1 answer

After some debugging in the chat, we found a problem. I will share the solution for others.

Websocket sent messages in a string format. So the data in the javascript code was a string, not a javascript object (JSON). Ember Data expects us to click on a javascript object, not a string.

The solution is to first analyze the response from websocket to the javascript object before inserting it into the repository, for example by doing JSON.parse(data) .

+8
source

Source: https://habr.com/ru/post/1212851/


All Articles