Ember-data “Cannot call method 'hasOwnProperty' of undefined”

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

I have the following Ember.js model:

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

In my app.js I have the following settings for the model (loaded prior to the model):

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

When loading the page, an AJAX call to the milestones list is done. The following JSON is returned:

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

When the JSON is loaded, the following error is returned:

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) 

What am I doing wrong? I have tried following the same structure as https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/adapter/rest_adapter_test.js#L465. If I rename the milestones key it says that no model could be found for the new key, so something must be right :-)

Thanks in advance.

Edit

The route that I use is the following:

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

If this.store.find('milestone') is not called the error doesn't occur. Also, if I use the FixtureAdapter instead of the RESTAdapter, the error still occurs. So it seems that this might be related to the model, not the data source.

回答1:

The problem was caused by the id attribute in the model. Apparently this should not be part of the model.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!