0

我正在使用 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 响应。

4

1 回答 1

2

您的呼吁:

http://localhost:4200/api/users

不能回来exactly the same JSON response

您的要求:http://localhost:4200/api/users/1应该返回:

{
  "data": {
    "id":"1",
    "type": "user",
    "attributes" : {
      "firstname":"John",
      "lastname":"Doe",
      "email":"johndoe@example.com",
      "mobile":"12345678",
      "nextAppointment":1
    }
  }
}

阅读有关 JSONAPIAdapter 的更多信息:

于 2015-09-21T13:19:41.087 回答