Ember数据 - 无法读取未定义的属性'typeKey'

时间:2014-07-31 21:42:33

标签: json ember.js ember-data

尝试将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.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");
    }
  })
});

应用/模型/ 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没有找到响应的模型。我可以确认它找到了app类型,在normalizeHash.apps下触发了一个钩子。

对不起,这是一篇很长的帖子,只是无法解决问题的原因!

2 个答案:

答案 0 :(得分:1)

App.Thing = DS.Model.extend(
    {
        name: attr('string'),
        children: DS.hasMany('child', {inverse:null})
    }
);

App.ThingSerializer = DS.RESTSerializer.extend(
    DS.EmbeddedRecordsMixin, {
        attrs: {
            children: { embedded: 'always' }
        }
    }
);

DS.EmbeddedRecordsMixin必须在您的模型中,并且您必须具有`embedded:'always'以获得正确的属性。

如果您有Thing模型,则可以使用特定于模型的序列化程序使Ember Data加载嵌入的子项(此处和嵌套对象数组)。

资源:

答案 1 :(得分:-1)

Ember不希望将记录嵌入父记录的JSON中。 做你需要的,让你的json像这样。只有计划ID。

{
  "apps":[
    {
      "_id":"53da9994b2878d0000a2e68f",
      "name":"Myapp",
      "domain":"http://myapp.com",
      "creator":"53d9598bb25244e9b1a72e53",
      "plan_id":"53d93c44b760612f9d07c921",    // just output id only not embedded record 
      "time_stamp":"2014/07/31 13:31:32",
      "__v":0
    }
  ]
}

然后让ember使用async:true

查找相关模型本身
export default DS.Model.extend({
  name:    attribute('string'),
  domain:  attribute('string'),
  plan:    DS.belongsTo('plan', { async: true }),     //changed
  creator: DS.belongsTo('user', { async: true }),

  time_stamp: attribute('string', {
    defaultValue: function () {
       return moment().format("YYYY/MM/DD HH:mm:ss");
    }
  })

});

我刚刚经历了这种痛苦,并在一些帮助下找到了答案。

对于其他任何来到这里仍然存在问题的人,请阅读我对自己问题的回答,详细了解typeKey错误的含义以及我自己用来解决问题的其他步骤。

Deploying ember-rails to Heroku - TypeError: Cannot read property 'typeKey' of undefined