ember JSONAPIAdapter 无法将数据正确加载到存储中
ember JSONAPIAdapter fails to load data properly into store
我正在使用 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}}
响应数据看起来不错,但问题是与响应一起的是 header 状态代码 304,以及数据未加载到存储中的事实。创建了 id=1 的 object,但是当我在 ember 检查器中查看商店数据时,object 中的所有属性都是 'undefined'。
更新:
我得到的错误是:
Error while processing route:
home.index Cannot read property '_internalModel' of undefined
TypeError: Cannot read property '_internalModel' of undefined
更新二:
事实证明 304 并不重要。当 httpcode 为 200 时,模型仍未正确加载到商店中。
我也资助这个电话工作正常:
http://localhost:4200/api/users
此调用失败时:
http://localhost:4200/api/users/1
他们return完全一样JSON反应。
您的电话:
http://localhost:4200/api/users
不能returnexactly the same JSON response
.
您的呼吁:http://localhost:4200/api/users/1
应该 return:
{
"data": {
"id":"1",
"type": "user",
"attributes" : {
"firstname":"John",
"lastname":"Doe",
"email":"johndoe@example.com",
"mobile":"12345678",
"nextAppointment":1
}
}
}
阅读有关 JSONAPIAdapter 的更多信息:
我正在使用 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}}
响应数据看起来不错,但问题是与响应一起的是 header 状态代码 304,以及数据未加载到存储中的事实。创建了 id=1 的 object,但是当我在 ember 检查器中查看商店数据时,object 中的所有属性都是 'undefined'。
更新:
我得到的错误是:
Error while processing route:
home.index Cannot read property '_internalModel' of undefined
TypeError: Cannot read property '_internalModel' of undefined
更新二: 事实证明 304 并不重要。当 httpcode 为 200 时,模型仍未正确加载到商店中。 我也资助这个电话工作正常:
http://localhost:4200/api/users
此调用失败时:
http://localhost:4200/api/users/1
他们return完全一样JSON反应。
您的电话:
http://localhost:4200/api/users
不能returnexactly the same JSON response
.
您的呼吁:http://localhost:4200/api/users/1
应该 return:
{
"data": {
"id":"1",
"type": "user",
"attributes" : {
"firstname":"John",
"lastname":"Doe",
"email":"johndoe@example.com",
"mobile":"12345678",
"nextAppointment":1
}
}
}
阅读有关 JSONAPIAdapter 的更多信息: