显示JSON Api符合错误 [英] Display JSON Api conform errors
问题描述
我从后端收到 JSON Api一致错误:
{
"errors": [
{
"status": "400",
"source": {
"pointer": "/data/attributes/description"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/due-date"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/extra-comments"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/name"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/payment-type"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/price"
},
"detail": "This field may not be null."
}
]
}
我尝试显示他们我的模板,如 EmberData文档所述:
I try to show them in my template, as described in the EmberData documentation:
{{#each model.errors.messages as |message|}}
<div class="error">
{{message}}
</div>
{{/each}}
没有显示任何内容。我会说模型中的 .errors
没有填充,但我不知道如何检查。如何:
Nothing is shown. I would say the .errors
in the model are not populated, but I am not sure how to check this. How can I:
- 显示收到的ajax回复?
- 确保EmberData正在处理回复并填充
model.errors
? - 显示已处理的
model.errors
在控制台中? - 显示
模型
和所有属性?
- display the received ajax reply?
- make sure that EmberData is processing the reply and populating
model.errors
? - Show the processed
model.errors
in the console? - Show the
model
and all properties?
一般来说,我遇到新版本的Ember很难调试。每当我在控制台中显示任何Ember对象时,我只是看到一些计算的
属性,只要我试图窥视它们就不会扩展。
In general, I am experiencing that new versions of Ember are very hard to debug. Whenever I show any Ember object in the console, I just see some Computed
properties which are not expanded whenever I try to peek into them.
我的后端是:
- Django 1.9
- with django-rest-framework
- django-rest-framework-json-api
- Django 1.9
- with django-rest-framework
- django-rest-framework-json-api
这是我发送到后端的数据(JSONAPi一致):
This is the data that I am POSTing to the backend (JSONAPi conform):
{
"data": {
"attributes": {
"name": null,
"description": null,
"extra-comments": null,
"min-price": 30,
"max-price": 3000,
"price-step": 10,
"price": null,
"payment-type": null,
"due-date": null
},
"relationships": {
"seller": {
"data": null
},
"artist": {
"data": null
},
"subcategory": {
"data": null
}
},
"type": "projects"
}
}
后端是可以的,检测错误,并提供一个JSON APi符合错误
回复,如上所述。
The backend is ok with this, detects the errors, and provides a JSON APi conform errors
reply, as specified above.
推荐答案
我认为我知道发生了什么好)。
I think I know what's happening (as it happened to me as well).
将HTTP错误代码从400更改为422(不可处理的实体),并检查是否解决了问题。
Change the HTTP error code from 400 to 422 (Unprocessable Entity) and check if it fixes the problem.
另外,看看 JSONAPIAdapter(从RestAdapter扩展)的源代码我认为我是对的。
Also, looking at the source code for the JSONAPIAdapter (which extends from the RestAdapter) I think I'm right.
isInvalid: function(status, headers, payload) {
return status === 422;
},
可以更改为(适配器/应用程序.js
):
import DS from 'ember-data';
import config from '../config/environment';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
host: config.API_HOST,
namespace: config.API_NAMESPACE,
isInvalid: function(status, headers, payload) {
return status === 400 || status === 422;
},
});
这篇关于显示JSON Api符合错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!