Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js - Error - "Assertion failed: You must include an `id` in a hash passed to `push`"

Tags:

ember.js

I'm getting this error after I save a post (title, text) to a mongodb database via a REST API written with Express and refresh the browser. I've already set my primary key to '_id' and have been reading about possibly normalizing the data?

Here is the payload from the server (only 1 post in db):

{
  "posts": [
  {
    "title": "The Title",
    "text": "Lorem ipsum",
    "_id": "52c22892381e452d1d000010",
    "__v": 0
   }
  ]
}

The controller:

App.PostsController = Ember.ArrayController.extend({
    actions: {
      createPost: function() {
        // Dummy content for now
        var to_post = this.store.createRecord('post', {
          title: 'The Title',
          text: 'Lorem ipsum'
        });
        to_post.save();
      }
    } 
 });

The model:

App.Post = DS.Model.extend({
   title: DS.attr('string'),
   text: DS.attr('string')
});

Serializer:

App.MySerializer = DS.RESTSerializer.extend({
  primaryKey: function(type){
    return '_id';
  }
});

Adapter:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  namespace: 'api'
});

Any help is much appreciated! Please let me know if you need any other info. Thanks

like image 366
songawee Avatar asked Dec 31 '13 02:12

songawee


1 Answers

When using custom adapters/serializers the naming is important. If you want it to apply to the entire application it should be called ApplicationSerializer

App.ApplicationSerializer = DS.RESTSerializer.extend({
  primaryKey: '_id'
});

Adapter:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  namespace: 'api'
});

If you just want it to apply to a single model (this applies to adapter's as well)

App.PostSerializer = DS.RESTSerializer.extend({
  primaryKey: '_id'
});
like image 110
Kingpin2k Avatar answered Sep 28 '22 06:09

Kingpin2k