Ember关系:TypeError:无法设置undefined的属性'store'

时间:2014-04-03 17:41:59

标签: javascript ember.js

我的第一个带有Node.JS后端的Ember.js应用程序出现以下错误:

Intermediate-transitioned into 'loading' ember-1.5.0.js:3521
Error while loading route: TypeError: Cannot set property 'store' of undefined
at DS.Store.Ember.Object.extend.modelFor (http://127.0.0.1:5555/js/libs/ember-data.js:2986:31)
at DS.Store.Ember.Object.extend.recordForId (http://127.0.0.1:5555/js/libs/ember-data.js:2437:29)
at deserializeRecordId (http://127.0.0.1:5555/js/libs/ember-data.js:3355:35)
at http://127.0.0.1:5555/js/libs/ember-data.js:3333:21
at http://127.0.0.1:5555/js/libs/ember-data.js:7117:30
at http://127.0.0.1:5555/js/libs/ember-1.5.0.js:3428:30
at Object.OrderedSet.forEach (http://127.0.0.1:5555/js/libs/ember-1.5.0.js:3271:24)
at Object.Map.forEach (http://127.0.0.1:5555/js/libs/ember-1.5.0.js:3426:22)
at Function.DS.Model.reopenClass.eachRelationship (http://127.0.0.1:5555/js/libs/ember-data.js:7116:50)
at normalizeRelationships (http://127.0.0.1:5555/js/libs/ember-data.js:3319:18) ember-1.5.0.js:3521
Transition #0: games: transition was aborted 

/ game

返回以下json
{
  "games": [
    {
      "serverSeedHash": "5eb59053dcb5810322e367245f631a65589ddf7bbe950d2986a43a5f376312ef",
      "id": 2,
      "serverSeed": null,
      "table": [
        1
      ],
      "amountOfPlayers": 2,
      "prizePool": 39600
    }
  ],
  "tables": [
    {
      "id": 1,
      "minimumPlayers": 2,
      "maximumPlayers": 100,
      "cardPrice": 1000,
      "games": [
        2
      ]
    }
  ]
}

我的完整Ember.js app.js看起来像这样:

window.App = Ember.Application.create({
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: true
});

App.Router.map(function () {
    this.route('games', { path: '/' });
});

App.Game = DS.Model.extend({
    serverSeedHash: DS.attr(),
    serverSeed: DS.attr(),
    amountOfPlayers: DS.attr(),
    prizePool: DS.attr(),
    table: DS.belongsTo('table')
});

App.Table = DS.Model.extend({
    minimumPlayers: DS.attr(),
    maximumPlayers: DS.attr(),
    games: DS.hasMany('game')
});


App.GamesRoute = Ember.Route.extend({
    model: function () {
        return this.store.find('game');
    }
});

有没有人知道我为什么会收到错误?请注意这是我第一次玩弄Ember.JS。 在过去的几天里,我一直在打破这种态度。

1 个答案:

答案 0 :(得分:1)

在您的game模型中,您将table定义为belongsTo关系,因此Ember希望它在您发送数组时是单数的。将您的对象更改为此,它应该可以工作:

{
  "games": [
    {
      ...
      "table": 1,
      ...
    }
  ],
  "tables": [
    ...
  ]
}