我的 RESTAdapter 配置:
App.ApplicationAdapter = DS.RESTAdapter.extend
namespace: '/api'
我的模型:
module.exports = App.Cat = DS.Model.extend
name: DS.attr 'string'
description: DS.attr 'string'
picture: DS.attr 'string'
location: DS.attr 'string'
profileStyle: DS.attr 'string'
created: DS.attr 'date'
我的路线:
Cat = require '../../models/cat'
App.CatNewRoute = Em.Route.extend
model: ->
@store.createRecord(Cat, {
created: new Date()
})
我的 Controller :
App.CatNewController = Em.ObjectController.extend
actions:
addCat: ->
console.log 'saving...'
@get('model').save().then ->
console.log 'all done'
我已经通过 Ember 检查器验证我的模型具有在保存时通过模板上的表单分配的所有属性(除了 id 为 null - 我认为这是正常的,默认情况下是 Ember使用服务器分配给模型的 ID)。每当我保存模型时,我都会收到错误消息:
Uncaught TypeError: Cannot read property 'typeKey' of undefined
ember 数据源代码中出现此错误:
if (container) {
adapter = container.lookup('adapter:' + type.typeKey) || container.lookup('adapter:application');
}
我使用的是 Ember-data 1.0.0 beta 3-2 和 Ember 1.0.0-4。我已经尝试了几个不同版本的 Ember 数据来达到相同的效果。
jsBin 中的近似值:http://jsbin.com/ixIqIjI/6/edit?html,js,console,output
这个 jsBin 也有 typeKey 错误。
最佳答案
我找到了解决方案。 createRecord
接受type
和record
参数,但是type
不能是像App 这样的显式模型名称.Cat
,它必须是一个字符串,Ember 然后根据名称附加到正确的模型。在这种情况下,创建记录的正确方法是 this.store.createRecord('cat', {...} )
关于ember.js - Ember 错误 "Uncaught TypeError: Cannot read property ' typeKey' of undefined "on model save,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19167831/