【发布时间】:2017-03-21 18:18:59
【问题描述】:
我刚刚将一个应用从 Ember 1.13 升级到 Ember 2.12。现在我加载数据存储的尝试失败了。这一行:
return this.store.find('blog', 14);
生成此错误:“处理路由时出错:blogs.index 断言失败:您对 ID 为 14 的博客发出了findRecord 请求,但适配器的响应没有任何数据”。
但是数据正在到达,并且格式如下:
{ “博客”:{ “身份证”: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的开发者工具中查看时返回的payload(联系人表只有一条记录):
{
"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)
【问题讨论】:
-
你的序列化器怎么样,它扩展了emberjs.com/api/data/classes/DS.RESTSerializer.html。
find现在是私有方法。考虑使用 findRecord emberjs.com/api/data/classes/DS.Store.html#method_findRecord -
谢谢,我没有指定序列化器,因为 RESTAdapter 是 Ember 1.13 中的默认适配器。所以我创建了一个: import DS from 'ember-data';导出默认 DS.RESTSerializer.extend({ });
-
添加序列化程序不影响报错,将store.find()改为store.findRecord()也没有
-
我对这个属性有疑问
person_id,你是如何在模型文件中定义它的?我知道 JSON 响应服务器应该返回person-id但我不确定 REST -
好点。我的模型使用下划线。我会进一步调查。也许我需要转换?