0
votes

Getting this error . Not sure why i am getting this error....would appreciate if someone can help me spot why this is erroring out.

Error while loading route: Error: Assertion Failed: You must include an id in a hash passed to push at new Error (native) at Error.Ember.Error

from the other posts related to similar error this has to do with a json where primary key is not handled correctly. But my json response looks correct.

 ****here are model objects:**** 
var PersonInfo = DS.Model.extend({
  first: DS.attr('string'),
  last : DS.attr('string'),
  addresses: DS.hasMany('personAddress', {embedded: 'always'})
 });
 Ember.Inflector.inflector.irregular("personInfo", "peopleInfo");
 export default PersonInfo;

 var Address = DS.Model.extend({
  type: DS.attr('string'),
  personInfo: DS.belongsTo('personInfo')
 });
 export default Address;

 ****here is my deserializer:****


 var PersonInfoSerializer = DS.ActiveModelSerializer.extend({
  primaryKey: 'id',
  extractArray: function(store, type, payload, id, requestType) {   
      var peopleInfo =payload.peopleInfo;              
      var adds = [];
     // debugger;
      peopleInfo.forEach(function(personInfo){              
           var addresses = personInfo.addresses,
                addressIds = addresses.mapProperty('id');                         
              adds.push(addresses);
              personInfo.addresses = addressIds;
      });             
      payload.addresses = adds;
      return this._super(store, type, payload, id, requestType);      }
       });
      export default PersonInfoSerializer;

     ****and here is the json response which i am mocking in API STUB****



  server.get('/peopleInfo', function(req, res) {
                var person_info = {
                 "peopleInfo": [{
                   "id": "1",
                   "first": "Tom",
                   "last": "Dale",
                   "addresses": [{
                     "id": "1",
                     "type": "Home"
                   }, {
                        "id": "2",
                      "type": "Work"
                   }]
                 }]
               };
    res.send(person_info);
            });
1
Is there any reason you're using the active model serializer? - Kingpin2k

1 Answers

0
votes

I'm not sure why you were using the ActiveModelSerializer, but it doesn't really buy you anything if your data isn't coming down in the format that Rails generally provides.

You're data wasn't being formatted correctly. Additionally there is no need to write {embedded:'always'} that does nothing anymore. You'll probably want to look at the transition document https://github.com/emberjs/data/blob/master/TRANSITION.md .

App.PersonInfoSerializer = DS.RESTSerializer.extend({
  extractArray: function(store, type, payload, id, requestType) {   
      var peopleInfo =payload.peopleInfo;              
      var adds = [];

      peopleInfo.forEach(function(personInfo){    
        //debugger;
           var addresses = personInfo.addresses,
              addressIds = addresses.getEach('id');  

              adds = adds.concat(addresses);
              personInfo.addresses = addressIds;
      });             

      payload.personAddresses = adds;
      return this._super(store, type, payload, id, requestType);      
  }
});

http://emberjs.jsbin.com/OxIDiVU/477/edit