ember.js - Ember 错误 "Uncaught TypeError: Cannot read property ' typeKey' of undefined "on model save

标签 ember.js

我的 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 接受typerecord 参数,但是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/

相关文章:

ember.js - 如何使用 Ember 2.x 设置从模型中检查的复选框属性?

javascript - 如何设置模型的加载顺序以避免关系错误?

ember.js - 自 RC3.1 以来,{{action}} 行为发生了什么变化,破坏了 ember.js todo 代码

jquery - 在转换到路由之前跳转到页面顶部

ember.js - 如何自定义 application.hbs 模板的 View 元素?

javascript - Ember1.0版本,如何从事件点击中获取整个对象(类比0.9版本的event.context)?

css - 如何让 Ember.js 绑定(bind) Attr 刷新?

ember.js - 序列化异步有很多关系

javascript - Ember js - 应用程序路由中的模型未加载

unit-testing - 如何在 ember-simple-auth 中注入(inject)单元测试 session ?