gpt4 book ai didi

javascript - EmberJS : Cannot read property 'typeKey' of undefined

转载 作者:搜寻专家 更新时间:2023-11-01 04:50:17 24 4
gpt4 key购买 nike

我想这个应该相当简单,但我还没有找到问题的根源,我还在学习 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)

如果我从我的 User 模型中删除或重命名 hasMany 关系,错误就会消失,所以我认为我的 JSON 构造不正确。

最佳答案

更新

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?

关于javascript - EmberJS : Cannot read property 'typeKey' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25213719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com