我的Ember.js应用中出现以下错误。
Error while processing route: books Cannot set property 'store' of undefined TypeError: Cannot set property 'store' of undefined
at DS.Store.Ember.Object.extend.modelFor (http://localhost:8080/js/libs/ember-data.js:2986:19)
at DS.Store.Ember.Object.extend.recordForId (http://localhost:8080/js/libs/ember-data.js:2437:17)
at deserializeRecordId (http://localhost:8080/js/libs/ember-data.js:3355:23)
at deserializeRecordIds (http://localhost:8080/js/libs/ember-data.js:3369:5)
at http://localhost:8080/js/libs/ember-data.js:3335:7
at http://localhost:8080/js/libs/ember-data.js:7117:16
at http://localhost:8080/js/libs/ember.js:14899:20
at Object.OrderedSet.forEach (http://localhost:8080/js/libs/ember.js:14741:14)
at Object.Map.forEach (http://localhost:8080/js/libs/ember.js:14897:14)
at Function.DS.Model.reopenClass.eachRelationship (http://localhost:8080/js/libs/ember-data.js:7116:38)
我正在使用
我有以下项目:
window.App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:8080'
});
App.Router.map(function() {
this.resource('books', { path: '/' });
});
App.BooksRoute = Ember.Route.extend({
model: function() {
return this.store.find('book');
}
});
App.Author = DS.Model.extend({
firstname: DS.attr('string'),
lastname: DS.attr('string')
});
App.Book = DS.Model.extend({
title: DS.attr('string'),
authors: DS.hasMany('author')
});
以下是我对http://localhost:8080/books
的JSON回复。
{
"books":
[
{
"id": 0,
"authors":
[
{
"id": 0,
"firstname": "Andrzej",
"lastname": "Sapkowski"
}
],
"title": "Zaklínač I: Poslední přání"
},
...
]
}
当我删除Author
模型和authors
关系声明时,应用程序正常工作。
我收到的错误消息并未显示原因,根据我在互联网上发现的内容,我的代码似乎没问题。
有什么问题?
答案 0 :(得分:1)
哇,我无法相信我没有注意到这一点,但您的数据格式不正确。看起来应该是这样的:
{
"books": [{}, {}, {}],
"authors": [{}, {}, {}]
}
REST adapter guide中解释了这一点。