在模型钩子中,this.store.find()失败

时间:2014-07-15 23:01:51

标签: ember.js ember-data

我这样做:

App.AccountsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('Account', {company: 'Blah'});
  }
});

但始终收到错误消息:

Error while processing route: accounts Cannot read property 'map' of undefined TypeError: Cannot read property 'map' of undefined
    at s.map (http://localhost/public/bower_components/ember/ember.min.js:10:14956)
    at Ember.Object.extend.pushMany (http://localhost/public/bower_components/ember-data/ember-data.min.js:10:6083)
    at c.extend.load (http://localhost/public/bower_components/ember-data/ember-data.min.js:9:19306)
    at http://localhost/public/bower_components/ember-data/ember-data.min.js:9:31635
    at a (http://localhost/public/bower_components/ember/ember.min.js:12:1824)
    at i (http://localhost/public/bower_components/ember/ember.min.js:12:1743)
    at c (http://localhost/public/bower_components/ember/ember.min.js:12:2476)
    at http://localhost/public/bower_components/ember/ember.min.js:13:11367
    at e.invoke (http://localhost/public/bower_components/ember/ember.min.js:11:24893)
    at Object.e.flush (http://localhost/public/bower_components/ember/ember.min.js:11:25401) ember.min.js:10
n.function.n.apply.r ember.min.js:10
A.error ember.min.js:18
i ember.min.js:18
s ember.min.js:19
e.trigger ember.min.js:19
(anonymous function) ember.min.js:19
a ember.min.js:12
i ember.min.js:12
h ember.min.js:12
(anonymous function) ember.min.js:13
e.invoke ember.min.js:11
e.flush ember.min.js:11
t.end ember.min.js:11
(anonymous function)

如果我摆脱了查询参数,那就是:

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

然后一切正常。我使用的是ember 1.6.0和ember-data 1.0.0-beta8。我已经尝试了几个不同的版本,以防它出现问题,但无法解决问题。

任何想法有什么不对?

1 个答案:

答案 0 :(得分:0)

来自查询/查找所有响应的json响应应该是一个集合。在你的情况下,它应该是这样的:

{
  accounts:[
    {
       id:1,
       company:'Blah'
    },
    {
       id:2,
       company:'Blah'
    }
    ...
  ]
}

另外,传入类型时应使用小写字母。

this.store.find('account');