Ember JS:断言失败:`AdapterError` 需要 json-api 格式的错误数组
Ember JS: Assertion Failed: `AdapterError` expects json-api formatted errors array
我正在使用 JSONAPIAdapter 制作我的第一个 Ember/Phoenix 应用程序。当执行 post 请求时 Ember 响应 Assertion Failed: AdapterError expects json-api formatted errors array.
相关代码如下:
adapter/application.js
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
import config from '../config/environment';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
});
serializers/application.js
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({
});
请求负载:
{
"data": {
"attributes": {
"name": ""
},
"relationships": {
"user": {
"data": {
"type": "users",
"id": "1"
}
}
},
"type": "listings"
}
}
响应负载:
{"errors":{"name":["can't be blank"]}}
为什么 Ember 一直给我这个错误?
您的响应负载不正确。您正在使用 json-api。所以你的有效载荷必须遵循 json-api specification. Your request-payload looks correct. But check out how errors have to be serialized。
json-api 错误响应必须包含一个包含错误对象数组的根键 "errors"。关于documenation一个错误可能包含几个member,但最重要的两个是detail和source。
这是一个例子:
{
"errors":[
{
"detail": "can't be blank",
"source": {
"pointer": "data/attributes/name"
}
}
]
}
source-key 包含一个 json-object 持有一个 json-api-pointer。借助此 pointer 信息 ember 的 JSONAPI-Adapter 将错误添加到记录的相应属性中。请注意,您的后端需要发送 422 HTTP 状态代码(无法处理的实体)。
如果可行,您可以在客户端做类似的事情:
{{#each model.errors.name as |error|}}
<div class="error">
{{error.message}}
</div>
{{/each}}
你如何在后端序列化你的错误?你在使用 ja_serializer 吗?如果没有,我会推荐它,因为 ja_serializer 默认可以序列化 json-api-errors.
wuarmin 已经链接的 documentation 明确指出(大写是他们的,斜体是我的):
Error objects MUST be returned as an array keyed by errors
您的 API 响应' errors
键具有单个对象作为值,而不是数组。
我正在使用 JSONAPIAdapter 制作我的第一个 Ember/Phoenix 应用程序。当执行 post 请求时 Ember 响应 Assertion Failed: AdapterError expects json-api formatted errors array.
相关代码如下:
adapter/application.js
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
import config from '../config/environment';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
});
serializers/application.js
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({
});
请求负载:
{
"data": {
"attributes": {
"name": ""
},
"relationships": {
"user": {
"data": {
"type": "users",
"id": "1"
}
}
},
"type": "listings"
}
}
响应负载:
{"errors":{"name":["can't be blank"]}}
为什么 Ember 一直给我这个错误?
您的响应负载不正确。您正在使用 json-api。所以你的有效载荷必须遵循 json-api specification. Your request-payload looks correct. But check out how errors have to be serialized。 json-api 错误响应必须包含一个包含错误对象数组的根键 "errors"。关于documenation一个错误可能包含几个member,但最重要的两个是detail和source。
这是一个例子:
{
"errors":[
{
"detail": "can't be blank",
"source": {
"pointer": "data/attributes/name"
}
}
]
}
source-key 包含一个 json-object 持有一个 json-api-pointer。借助此 pointer 信息 ember 的 JSONAPI-Adapter 将错误添加到记录的相应属性中。请注意,您的后端需要发送 422 HTTP 状态代码(无法处理的实体)。
如果可行,您可以在客户端做类似的事情:
{{#each model.errors.name as |error|}}
<div class="error">
{{error.message}}
</div>
{{/each}}
你如何在后端序列化你的错误?你在使用 ja_serializer 吗?如果没有,我会推荐它,因为 ja_serializer 默认可以序列化 json-api-errors.
wuarmin 已经链接的 documentation 明确指出(大写是他们的,斜体是我的):
Error objects MUST be returned as an array keyed by errors
您的 API 响应' errors
键具有单个对象作为值,而不是数组。