【问题标题】:Ember Data + JSONAPI - Error: The adapter rejected the commit because it was invalidEmber Data + JSONAPI - 错误:适配器拒绝提交,因为它无效
【发布时间】:2017-04-15 00:05:55
【问题描述】:

我正在尝试通过 POST 请求向使用 Ember Data 2.10 的基于 JSONAPI 的 API 提交无效数据。

API 在响应中使用422 代码和此有效负载正确响应(请注意,这些是error objects,而不是“正常” JSONAPI 有效负载响应):

{
  "errors": [{
    "title": "Title can't be blank",
    "id": "title",
    "code": "100",
    "source": {
      "pointer": "/data/attributes/title"
    },
    "status": "422"
  }, {
    "title": "Layout can't be blank",
    "id": "layout",
    "code": "100",
    "source": {
      "pointer": "/data/relationships/layout"
    },
    "status": "422"
  }, {
    "title": "Page type can't be blank",
    "id": "page-type",
    "code": "100",
    "source": {
      "pointer": "/data/attributes/page-type"
    },
    "status": "422"
  }]
}

错误似乎大部分都可以正常加载到模型中,但我在控制台中收到此错误:

ember.debug.js:19160 Error: The adapter rejected the commit because it was invalid
  at ErrorClass.EmberError (ember.debug.js:19083)
  at ErrorClass.AdapterError (errors.js:23)
  at ErrorClass (errors.js:49)
  at Class.handleResponse (rest.js:821)
  at Class.handleResponse (data-adapter-mixin.js:100)
  at Class.superWrapper [as handleResponse] (ember.debug.js:24805)
  at ajaxError (rest.js:1342)
  at Class.hash.error (rest.js:916)
  at fire (jquery.js:3305)
  at Object.fireWith [as rejectWith] (jquery.js:3435)

是什么导致了这个错误?服务器返回的 JSON 有效负载有问题吗?最近改变的一件事是引入了指向/data/relationships/layout 的指针; Ember Data 会因此而窒息吗?

我还可能注意到,通过PATCH 请求提交类似的不良数据不会在控制台中触发此错误。

主要问题是这会导致验收测试失败,我似乎找不到解决方法。能够在应用程序中测试此行为会很好,但我现在只需将其注释掉即可。

在更新到 2.10 之前,我也在 E​​mber Data 2.7 上尝试过这个,看看是否能解决这个问题。两个版本都出现同样的错误。

【问题讨论】:

  • 你的json好像不是JSON API 1.0格式
  • @MilkyWayJoe 是什么导致它不符合 JSON API 1.0?
  • 格式似乎不正确。你必须让你的 Rails 发送类似于"data": [{ "type": "error", "id": "title", "attributes": { "title": "Title can't be blank", "code": "100" }, {"type": "error" ... 的东西等等。注意到结构的不同了吗?
  • @MilkyWayJoe 我遇到的问题是错误对象,它们是另一种动物。 jsonapi.org/examples/#error-objects
  • 另外,适配器不应该正确拒绝无效的 POST 吗?你确定它不只是告诉你它正在正确地完成它的工作吗?在非开发环境中你会得到什么?

标签: ember.js ember-data json-api


【解决方案1】:

由于您的应用使用默认适配器,因此请确保遵循 JSON:API 约定来构建响应

here提供少量指南

【讨论】:

  • 您的回答没有帮助。我在我的问题中发布了有效负载。具体有什么问题?
  • 您的错误负载是正确的。我认为你在正确的道路上。这是 ember 的正常行为。 Ember Data 通知您模型未成功保存,因为服务器以 422 响应
猜你喜欢
  • 2016-04-19
  • 2019-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
相关资源
最近更新 更多