Ember.js - 加载路由时出错:TypeError:无法设置undefined的属性'store'

时间:2014-10-26 15:14:46

标签: rest ember.js ember-data relationships

我的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) 

我正在使用

  • ember.js版本1.7.1和
  • ember-data.js版本1.0.0-beta.5。

我有以下项目:

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关系声明时,应用程序正常工作。

我收到的错误消息并未显示原因,根据我在互联网上发现的内容,我的代码似乎没问题。

有什么问题?

1 个答案:

答案 0 :(得分:1)

哇,我无法相信我没有注意到这一点,但您的数据格式不正确。看起来应该是这样的:

{
    "books": [{}, {}, {}],
    "authors": [{}, {}, {}]
}

REST adapter guide中解释了这一点。