无法设置未定义的 属性 'all'

Cannot set property 'all' of undefined

我在 Ember.JS 应用程序中从 API 获取响应时遇到问题。我正在使用 "dummy" API just to learn Ember with and I didn't feel like creating my own back end (this one specifically).

每当我尝试导航到 Posts 模板时,我都会收到以下错误:

Error while processing route: posts Cannot read property 'all' of undefined TypeError: Cannot read property 'all' of undefined
    at App.PostsRoute.Ember.Route.extend.model (file:///C:/Users/staff-ch/Documents/ember/js/app.js:24:18)
    at EmberObject.default.extend.deserialize (file:///C:/Users/staff-ch/Documents/ember/js/libs/ember-1.12.0.debug.js:22318:19)
    at applyHook (file:///C:/Users/staff-ch/Documents/ember/js/libs/ember-1.12.0.debug.js:45113:32)
    at Object.HandlerInfo.runSharedModelHook (file:///C:/Users/staff-ch/Documents/ember/js/libs/ember-1.12.0.debug.js:43114:22)
    at Object.subclass.getModel (file:///C:/Users/staff-ch/Documents/ember/js/libs/ember-1.12.0.debug.js:43340:21)
    at __exports__.bind (file:///C:/Users/staff-ch/Documents/ember/js/libs/ember-1.12.0.debug.js:44982:19)
    at tryCatch (file:///C:/Users/staff-ch/Documents/ember/js/libs/ember-1.12.0.debug.js:45439:16)
    at invokeCallback (file:///C:/Users/staff-ch/Documents/ember/js/libs/ember-1.12.0.debug.js:45451:17)
    at publish (file:///C:/Users/staff-ch/Documents/ember/js/libs/ember-1.12.0.debug.js:45422:11)
    at file:///C:/Users/staff-ch/Documents/ember/js/libs/ember-1.12.0.debug.js:26472:7

相关代码如下:

App = Ember.Application.create();

App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://jsonplaceholder.typicode.com/'
})

App.Router.map(function() {
  ...
  this.resource('posts');
});

App.Post = DS.Model.extend({
    userId: DS.attr(),
    title: DS.attr(),
    body: DS.attr()
});

App.PostsRoute = Ember.Route.extend({
    model: function(){
        return DS.store.all('posts');
    }
});

我怀疑这可能与返回的 JSON 的格式有关,但我不确定,如果是这种情况,我也不确定如何修复它。我显然无法更改正在返回的 JSON 的格式,但是我知道 DS.RESTSerilaizer 但我不确定如何使用它。响应示例:

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },...
]

1) 只是

App.PostsRoute = Ember.Route.extend({
  model: function(){
    return this.store.all('post');
  }
});

改为return DS.store.all('posts');

DS.Store 作为 store 属性.

注入路由

2) 您的回复必须有根 posts.