我想这个应该相当简单,但我还没有找到问题的根源,我还在学习Ember-Data。
我有两个模型,它们之间有hasMany
的关系:
App.User = DS.Model.extend({
displayName: DS.attr('string'),
email: DS.attr('string'),
firstName: DS.attr('string'),
lastName: DS.attr('string'),
location: DS.attr('string'),
messages: DS.hasMany('message')
});
App.Message = DS.Model.extend({
user: DS.belongsTo('user'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date'),
fullText: DS.attr('string'),
subject: DS.attr('string'),
recipients: DS.attr('string')
});
Ember-data从服务器检索用户,输出如下:
{
"user":[
{
"id":"3",
"firstName":"A",
"lastName":"User",
"location":"",
"email":"a@user.com",
"displayName":"auser",
"messages":[
{
"id":"3",
"user":"3",
"createdAt":"2014-08-06 20:08:38",
"fullText":"Here is some text",
"recipients":"",
"subject":"Message subject (may not be needed)",
"updatedAt":"2014-08-06 20:08:38"
}
]
}
]
}
但是,当Ember尝试使用此响应填充商店时,我收到以下错误:
Error while loading route: TypeError: Cannot read property 'typeKey' of undefined
at Ember.Object.extend.modelFor (http://www.musicicons.dev/assets/packages/ember-data/ember-data.js:10667:22)
at Ember.Object.extend.recordForId (http://www.musicicons.dev/assets/packages/ember-data/ember-data.js:10112:21)
at deserializeRecordId (http://www.musicicons.dev/assets/packages/ember-data/ember-data.js:11116:27)
at deserializeRecordIds (http://www.musicicons.dev/assets/packages/ember-data/ember-data.js:11130:9)
at http://www.musicicons.dev/assets/packages/ember-data/ember-data.js:11095:11
at http://www.musicicons.dev/assets/packages/ember-data/ember-data.js:9317:20
at http://www.musicicons.dev/assets/packages/ember/ember.js:3428:16
at Object.OrderedSet.forEach (http://www.musicicons.dev/assets/packages/ember/ember.js:3271:10)
at Object.Map.forEach (http://www.musicicons.dev/assets/packages/ember/ember.js:3426:10)
at Function.Model.reopenClass.eachRelationship (http://www.musicicons.dev/assets/packages/ember-data/ember-data.js:9316:42)
如果我从hasMany
模型中删除或重命名User
关系,则错误消失,所以我认为我的JSON构造不正确。
答案 0 :(得分:6)
Ember Data现在支持它:Ember-data embedded records current state?
默认情况下,Ember Data不支持嵌入式记录,这意味着你的json应该采用这种格式(如果你是通过id 3找到用户):
{
"user":
{
"id":"3",
"firstName":"A",
"lastName":"User",
"location":"",
"email":"a@user.com",
"displayName":"auser",
"messages": [3]
},
"messages":[
{
"id":"3",
"user":"3",
"createdAt":"2014-08-06 20:08:38",
"fullText":"Here is some text",
"recipients":"",
"subject":"Message subject (may not be needed)",
"updatedAt":"2014-08-06 20:08:38"
}
]
}
示例:http://emberjs.jsbin.com/OxIDiVU/918/edit
您可以在此处阅读有关嵌入式记录的信息:Ember-data embedded records current state?
答案 1 :(得分:6)
Ember Data现在支持嵌入式记录,因此您需要做的就是定义自定义序列化程序,Ember Data应该按原样接受您的JSON有效负载。以下是您需要定义的自定义序列化程序:
App.UserSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
messages: {embedded: 'always'}
}
});
这基本上告诉Ember Data'消息'将成为用户对象图的一部分。
如果你正在使用ActiveModelSerializer,那么它将会是这样的:
App.UserSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
messages: {embedded: 'always'}
}
});
答案 2 :(得分:2)
也许有人会发现这些信息很有用,因为这是谷歌第一次针对此错误按摩:
当您的模型看起来像这样时,也会发生此错误:
App.Project = DS.Model.extend({
customers: DS.belongsTo('customer')
});
属性采用复数形式,但使用的是belongsTo
而不是hasMany