我刚刚将应用程序从Ember 1.13升级到Ember 2.12。现在我尝试加载数据存储失败了。这一行:
return this.store.find('blog', 14);
生成此错误:"处理路由时出错:blogs.index断言失败:您对ID为14的博客发出了findRecord
请求,但是适配器的响应没有任何数据"
但是数据到了,并采用以下格式:
{ "博客":{ " id":14, " person_id":" 1", "访问":" 3" } }
我的适配器在application / adapter.js中指定为:
导出默认DS.RESTAdapter.extend({
主持人:" http://localhost", 命名空间:' api-rick'
});
任何人都知道我为什么会收到此错误消息?我认为JSON格式正确 - 在升级Ember之前没有任何问题。
稍后编辑,这里的相关代码:
//application/adapter.js
import DS from 'ember-data';
import Ember from 'ember';
export default DS.RESTAdapter.extend({
host: "http://localhost",
namespace: 'api-rick',
pathForType: function(type) {
//this so types (db table names) are singular;
//else are pluralized in Adapter's request
return Ember.String.singularize(type);
}
});
//application/serializer.js
port DS from 'ember-data';
export default DS.RESTSerializer.extend({
});
//pods/contact/model.js
import DS from 'ember-data';
export default DS.Model.extend({
name : DS.attr(),
});
//pods/contact/route.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.findAll('contact');
},
});
//在Chrome的开发者工具中查看时返回了有效负载(联系人表只有一条记录):
{
"contact": [
{
"id": 1,
"name": "Bilbo the duck"
}
]
}
最后,这是Chrome控制台中报告的错误:
Transition #0: contact: calling beforeModel hook
ember.debug.js:55891
Transition #0: contact: calling deserialize hook
ember.debug.js:28573
Error while processing route: contact Assertion Failed: You made a `findAll` request for contact records, but the adapter's response did not have any data Error
at assert (http://localhost:4200/assets/vendor.js:20732:13)
at Object.assert (http://localhost:4200/assets/vendor.js:32400:34)
at assert (http://localhost:4200/assets/vendor.js:85626:37)
at http://localhost:4200/assets/vendor.js:97389:41
at tryCatch (http://localhost:4200/assets/vendor.js:73561:14)
at invokeCallback (http://localhost:4200/assets/vendor.js:73576:15)
at publish (http://localhost:4200/assets/vendor.js:73544:9)
at http://localhost:4200/assets/vendor.js:53448:16
at invokeWithOnError (http://localhost:4200/assets/vendor.js:15377:16)
at Queue.flush (http://localhost:4200/assets/vendor.js:15436:9)
答案 0 :(得分:0)
如果你的后端用下划线回复,那么你需要有以下代码,
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
keyForAttribute: function(attr, method) {
return Ember.String.underscore(attr);
}
});