【问题标题】:Ember data - Cannot read property 'typeKey' of undefinedEmber 数据 - 无法读取未定义的属性“typeKey”
【发布时间】: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


【解决方案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 加载嵌入的子项(此处为嵌套对象数组)。

资源:

【讨论】:

    【解决方案2】:

    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

    【讨论】:

    • 不,DS.EmbeddedRecordsMixin 比创建异步关系要好得多。更少的延迟,以及整体负载大小和传输时间。
    • 我在这方面没有那么自信,如果我使用 embeded: load 而不是 async: true,我的其余建议是否有意义并且仍然有效?我更想说明 json 的外观并包含对我有用的异步。如果是这样,很高兴更新我的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多