ember-data-1.0.0 activemodeladapter error include an `id` in a hash passed to `push`
Asked Answered
J

1

6

I am using ember-data and the activemodel adapter with rails and mongoid(mongodb) in the backend. Whenever I make a request to my rails app, emberjs displays the returned data but in chrome developer console it displays:

Assertion failed: You must include an `id` in a hash passed to `push` 

the problem reproduced in a jsfiddle

I have reproduced the problem in this jsfiddle. You can see a different version of thesame jsfiddle working when I use id instead of _id.

Payload sent by the backend

ActiveModel adapter converts the snake_case start_date to camel-case. I have added some custom code to ember-data rest-serializer to convert _id to id and that code is pasted lower down in this question.

    {"schedules":[

       {
         "_id":"529f38596170700787000000",
         "name":"Hair styling",
         "start_date":"2013-12-12"
       }

     ]}

Now though the returned payload includes an id, if I go into the google cgrome console and run the commmands below and access the _data, it shows the id is undefined.

 a = App.__container__.lookup("controller:schedules")
 b = a.get('content')

I f use the disclosure arrows in the console and dig into the _dat object, this is what I see

 _data: Object
   id: undefined

Custom serializer code

I extended the activemodeladapter to convert mongodb's _id to id and to set it as primary key.

App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
   primaryKey: '_id',

   normalize: function(type, hash, prop) {
      this.normalizeMongoidId(hash);
      return this._super(type, hash, prop);
   },

   normalizeMongoidId: function(hash){
     var json = { id: hash._id };
     delete hash._id;

     //hash.id = hash._id;
     //delete hash._id;
   }

  /*
   serialize: function(record, options){
     var json = this._super(record, options);

      json.schedule._id  = json.record.id;

      delete json.record.id
      return json;
   }
  */ 
}); 

The ember-data model:

App.Schedule = DS.Model.extend({
  name: DS.attr('string'),
  organization: DS.belongsTo('organization')
});

Here is my emberjs router

App.Router.map(function(){ 
  this.resource('schedules', {path: "/schedules"}, function(){ 
   this.resource('schedule', {path: "/:schedule_id"}, function(){});   

  }); 
});

The emberjs route defination:

 App.SchedulesRoute = Ember.Route.extend({
   model: function(){
    return this.store.find('schedule');  
   }
 });

emberjs stack trace

   Assertion failed: You must include an `id` in a hash passed to `push`   application.js:31601
  (anonymous function) application.js:31601
  Ember.assert application.js:28431
  DS.Store.Ember.Object.extend.push application.js:69415
  (anonymous function) application.js:69483
  Ember.EnumerableUtils.map application.js:30115
  DS.Store.Ember.Object.extend.pushMany application.js:69482
  (anonymous function) application.js:69839
  invokeCallback application.js:36783
  (anonymous function) application.js:36838
  EventTarget.trigger application.js:36621
  (anonymous function) application.js:36921
  DeferredActionQueues.flush application.js:34001
  Backburner.end application.js:34092
  Backburner.run application.js:34131
  Ember.run application.js:34489
  hash.success application.js:74468
  fire application.js:3049
  self.fireWith application.js:3161
  done application.js:8236
  callback

Thanks

Journalism answered 12/12, 2013 at 13:4 Comment(7)
can you give full stack trace of error and response given by server to browserMint
The ember error is strange but from the log you have it looks like the error is on the Rails server (respond with error 500), could you please give us your schedulers_controller.rb ?Loreenlorelei
@AdrienCoquio the full schedules_controller.rb is pasted there under the section titled going from emberjs to railsJournalism
@Hardik127, the stack trace from the emberjs side is pasted underneath the question. For the rails side, what I pasted before is all the stack trace that was returned. I inspected the object returned in the chrome console and it had no id even the json it received had an id. this is the payload {"schedules":[{"id":{"$oid":"529f38596170700787000000"},"name":"Hair styling","description":null,"start_date":"2013-12-12","end_date":null,"organization":null}]}Journalism
I have reproduced the unresolved problem in a jsfiddle because I have solved all the backend related issues. However, the emberjs error is still being thrown. Here is the jsfiddle. You can see thesame jsfiddle working when I use id instead of _id.Journalism
On the server can your serializer convert _id to id ?Inventor
@Inventor thanks for contributing. I tried and the error was still return: You must include an id in a hash passed to push and indeed when I inspect the loaded data in the console, it's id is blank. do you mind playing with the jsfiddle added to my earlier comments to seeJournalism
T
0

I had the same issue. In your rails serializer for schedule, (assuming you have one), you should do

attributes :id, :name, :start_date

and have a

has_many :schedule 

in your SchedulesSerializer. Let me know if this works.

Turbit answered 26/3, 2014 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.