Ember嵌入的json错误#You必须在传递给push的哈希中包含一个id

时间:2014-05-12 22:23:29

标签: ember.js ember-data

获取此错误。不知道为什么我得到这个错误....如果有人可以帮助我发现为什么这是错误的,将不胜感激。

加载路由时出错:错误:断言失败:您必须在传递的哈希中包含一个id,以便在Error.Ember.Error 错误(本机)处推送

来自与类似错误相关的其他帖子,这与未正确处理主键的json有关。但我的json回答看起来是正确的。

 ****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 个答案:

答案 0 :(得分:0)

我不确定您使用ActiveModelSerializer的原因,但如果您的数据没有以Rails通常提供的格式出现,那么它并没有真正为您带来任何好处。

您的数据未正确格式化。此外,没有必要写{embedded:' always'}不再做任何事情。您可能希望查看转换文档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