【发布时间】:2014-07-31 21:42:33
【问题描述】:
尝试将plan 模型嵌入加载到我的app 模型中。
我在加载时不断收到以下错误(它保存得很好):
Cannot read property 'typeKey' of undefined TypeError: Cannot read property 'typeKey' of undefined
at Ember.Object.extend.modelFor (http://localhost:4200/assets/vendor.js:71051:22)
at Ember.Object.extend.recordForId (http://localhost:4200/assets/vendor.js:70496:21)
at deserializeRecordId (http://localhost:4200/assets/vendor.js:71500:27)
at http://localhost:4200/assets/vendor.js:71477:11
at http://localhost:4200/assets/vendor.js:69701:20
at http://localhost:4200/assets/vendor.js:17687:20
at Object.OrderedSet.forEach (http://localhost:4200/assets/vendor.js:17530:14)
at Object.Map.forEach (http://localhost:4200/assets/vendor.js:17685:14)
at Function.Model.reopenClass.eachRelationship (http://localhost:4200/assets/vendor.js:69700:42)
at normalizeRelationships (http://localhost:4200/assets/vendor.js:71463:12) vendor.js:17062logToConsole
话虽如此,我有以下型号,
app/models/app.js
export default DS.Model.extend({
name: attribute('string'),
domain: attribute('string'),
plan: DS.belongsTo('plan', { embedded: 'load' }),
creator: DS.belongsTo('user', { async: true }),
time_stamp: attribute('string', {
defaultValue: function () {
return moment().format("YYYY/MM/DD HH:mm:ss");
}
})
});
app/models/plan.js
export default DS.Model.extend({
price: attribute('number'),
description: attribute('string'),
tagline: attribute('string'),
title: attribute('string'),
features: attribute('array') // Array is defined in a transform, don't worry.
});
计划成为一种静态文档。
这是我调用store.get('creator.apps');时的服务器响应
{
"apps":[
{
"_id":"53da9994b2878d0000a2e68f",
"name":"Myapp",
"domain":"http://myapp.com",
"creator":"53d9598bb25244e9b1a72e53",
"plan":{
"_id":"53d93c44b760612f9d07c921",
"price":0,
"description":"Free plan",
"tagline":"Great for testing",
"title":"Developer",
"features":["5,000 Requests","API/Plugin Access"],
"__v":0
},
"time_stamp":"2014/07/31 13:31:32",
"__v":0
}
]
}
我意识到 typeKey 错误是由于 Ember 没有找到响应模型。我可以确认它找到了应用类型,在 normalizeHash.apps 下触发了一个钩子。
抱歉,这篇文章太长了,我无法理解问题的原因!
【问题讨论】:
-
抱歉,
{ embedded: 'load' }是什么,我在 ember-data 中从未见过?最近修复了 ember-data 中有关嵌入式 .如果您运行 ember-data 的金丝雀构建,您可以为您的模型创建一个序列化程序并包含DS.EmbeddedRecordsMixin,然后您在 attrs 哈希中指定嵌入属性attrs: {plan: {embedded: 'always'}}。希望这会有所帮助 -
@Altrim:例如。 stackoverflow.com/questions/14521182/… - 但我也尝试过。最后是:
TypeError: undefined is not a function at Ember.Mixin.create.extractArray (http://localhost:4200/assets/vendor.js:61286:25). -
您使用的是 ember-data canary 吗?如果您检查此提交 github.com/emberjs/data/commit/…,他们添加了对使用 EmbeddedRecordsMixin 和 JSONSerializer 的支持。我正在使用带有嵌入式数据的mixin,它工作正常。在使用金丝雀版本之前,我遇到了与您相同的错误。
-
Altrim:使用 EmbeddedRecordsMixin 升级到 ember 数据金丝雀。非常感谢。
标签: json ember.js ember-data