Ember-data"无法调用方法&has; hasOwnProperty'未定义"

时间:2014-02-24 14:39:09

标签: javascript ember.js ember-data

我有以下Ember.js模型:

Filters.Milestone = DS.Model.extend({
    id: DS.attr('string'),
    name: DS.attr('string')
});

在我的app.js中,我对模型有以下设置(在模型之前加载):

Filters.MilestoneSerializer = DS.RESTSerializer.extend();
Filters.MilestoneAdapter = DS.RESTAdapter.extend({
    namespace: "ember"
});

加载页面时,会完成对里程碑列表的AJAX调用。返回以下JSON:

{
    "milestones": [
        {
            "id": "1",
            "name": "Test Milestone #1"
        }
    ]
}

加载JSON时,会返回以下错误:

TypeError: Cannot call method 'hasOwnProperty' of undefined
    at e (http://sandbox.local/ember/js/libs/ember-data.js:8:30934)
    at null.<anonymous> (http://sandbox.local/ember/js/libs/ember-data.js:8:31181)
    at ComputedPropertyPrototype.set (http://sandbox.local/ember/js/libs/ember-1.4.0.js:4903:18)
    at new Class (http://sandbox.local/ember/js/libs/ember-1.4.0.js:12652:18)
    at Function.Mixin.create.create (http://sandbox.local/ember/js/libs/ember-1.4.0.js:13104:12)
    at Ember.Object.extend.buildRecord (http://sandbox.local/ember/js/libs/ember-data.js:10:2880)
    at Ember.Object.extend.recordForId (http://sandbox.local/ember/js/libs/ember-data.js:9:31214)
    at Ember.Object.extend._load (http://sandbox.local/ember/js/libs/ember-data.js:10:2048)
    at Ember.Object.extend.push (http://sandbox.local/ember/js/libs/ember-data.js:10:2450)
    at null.<anonymous> (http://sandbox.local/ember/js/libs/ember-data.js:10:2701)

我做错了什么?我尝试使用与https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/adapter/rest_adapter_test.js#L465相同的结构。如果我重命名里程碑键,它说没有找到新键的模型,所以必须正确: - )

提前致谢。

修改

我使用的路线如下:

Filters.FiltersRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('milestone')
    }
});

如果未调用this.store.find('milestone'),则不会发生错误。此外,如果我使用FixtureAdapter而不是RESTAdapter,则仍会出现错误。所以看起来这可能与模型有关,而不是与数据源有关。

1 个答案:

答案 0 :(得分:9)

问题是由模型中的id属性引起的。显然,这不应该是模型的一部分。