【问题标题】:Ember embedded json errors out #You must include an id in a hash passed to pushEmber 嵌入 json 错误 #You must include an id in a hash pass to push
【发布时间】:2014-05-12 22:23:28
【问题描述】:

收到此错误。不知道为什么我会收到此错误....如果有人可以帮助我找出为什么会出错,将不胜感激。

加载路由时出错:错误:断言失败:您必须在传递的哈希中包含一个 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);
            });

【问题讨论】:

  • 您有什么理由使用活动模型序列化程序吗?

标签: ember.js ember-data


【解决方案1】:

我不确定您为什么要使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2022-12-28
    • 2018-04-20
    • 1970-01-01
    • 2020-09-06
    • 2020-12-20
    • 2021-03-19
    相关资源
    最近更新 更多