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

只愿长相守 提交于 2019-12-08 19:46:23

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/20544371/ember-data-1-0-0-activemodeladapter-error-include-an-id-in-a-hash-passed-to-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!