Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "Assertion Failed: Expected an object as `data` in a call to `push`/`update`" when trying to normalize and push

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

Here's my code so far:

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 and normalizedData end up being exactly the same values, which is something like this:

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

And calling the push method provokes 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 don't know what is wrong. When Ember gets a specific message from the server, it receives the same kind of JSON and Ember Data deals well with it. When it comes from a websocket and needs to be pushed, it crashes.

Update

I tried to use pushPayload instead like it was proposed in the comments. It still doesn't work. I get those messages:

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

It goes up to number 67, then keeps going with words like fmt, camelize, htmlSafe... I'm pretty just data is just a string representing JSON.

like image 212
Marc-François Avatar asked Feb 07 '15 02:02

Marc-François


1 Answers

After we did some debugging in the chat we found the problem. I'll share the solution here for others.

The websocket was sending messages in string format. So the data in the javascript code was a string and not a javascript object (JSON). Ember Data expects us to push a javascript object and not a string.

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

like image 117
jcbvm Avatar answered Oct 31 '22 21:10

jcbvm