【发布时间】: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