我在Ember.JS应用程序中从API获取响应时遇到问题。我正在使用"虚拟" API只是为了学习Ember,我并不想创建自己的后端(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"
},...
]
答案 0 :(得分:0)
1)只是
App.PostsRoute = Ember.Route.extend({
model: function(){
return this.store.all('post');
}
});
代替return DS.store.all('posts');
DS.Store作为store
属性注入到路由中。
2)您的回复必须有根posts
。