Ember Data + JSONAPI - 错误:适配器拒绝提交,因为它无效

问题描述 投票:4回答:1

我试图通过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之前,我还在Ember Data 2.7上尝试了这个,看看是否能解决这个问题。两个版本都出现同样的错误。

ember.js ember-data json-api
1个回答
-1
投票

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

here提供了少量指南

© www.soinside.com 2019 - 2024. All rights reserved.