【发布时间】:2015-09-22 15:32:35
【问题描述】:
我正在使用 ember-cli 和 ember-data 1.13.7 和 JSONAPIAdapter。 我在本地测试期间使用 http-mock 来模拟数据。 当我使用 RESTAdapter 时它运行良好,但在切换到 JSONAPIAdapter 时遇到了问题。
问题是记录数据没有加载到存储中,读取未定义属性时出现错误。
适配器看起来像这样:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
namespace: 'api',
});
ajax 调用是这样的:
http://localhost:4200/api/users/1
http-mock 看起来像这样:
usersRouter.get('/:id', function(req, res) {
res.send({
'users': {
id: req.params.id,
firstname: "John",
lastname: "Doe",
email: "johndoe@example.com",
mobile: "12345678",
nextAppointment: 1
}
});
});
响应如下所示:
{"users":{"id":"1","firstname":"John","lastname":"Doe","email":"johndoe@example.com","mobile":"12345678","nextAppointment":1}}
响应数据看起来不错,但问题是与响应一起是 304 的标头状态代码,以及数据未加载到存储中的事实。创建了 id=1 的对象,但是当我在 ember 检查器中查看存储数据时,对象中的所有属性都是“未定义”。
更新:
我得到的错误是:
Error while processing route:
home.index Cannot read property '_internalModel' of undefined
TypeError: Cannot read property '_internalModel' of undefined
更新 2: 事实证明,304 并不重要。当 httpcode 为 200 时,模型仍未正确加载到存储中。 我还资助这个电话工作正常:
http://localhost:4200/api/users
当此调用失败时:
http://localhost:4200/api/users/1
它们返回完全相同的 JSON 响应。
【问题讨论】:
标签: ember.js ember-data json-api