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