【问题标题】:Errors not populated未填充错误
【发布时间】:2016-04-08 08:25:36
【问题描述】:

我从后端收到错误:

{
  "errors": {
    "extra_comments": [
      "This field may not be null."
    ],
    "name": [
      "This field may not be null."
    ],
    "due_date": [
      "This field may not be null."
    ],
    "price": [
      "This field may not be null."
    ],
    "payment_type": [
      "This field may not be null."
    ],
    "description": [
      "This field may not be null."
    ]
  }
}

我尝试在我的模板中显示它们:

{{#each model.errors.messages as |message|}}
  <div class="error">
    {{message}}
  </div>
{{/each}}

什么都没有显示。 EmberData 有问题吗? Ember 模板语法更改有问题吗? Ember 有问题吗?适配器?我的后台?不知道。问题面太大。我该怎么做:

  • 显示收到的ajax回复?
  • 确保 EmberData 正在处理回复并填充 model.errors
  • 在控制台中显示处理后的model.errors

总的来说,我发现 Ember 的新版本很难调试。每当我在控制台中显示任何 Ember 对象时,我只会看到一些 Computed 属性,当我尝试查看它们时不会计算这些属性。

编辑

我的后台是:

我不确定django-rest-framework-json-api 是否能够返回 JSON Api 一致性错误。我开了一个issue

【问题讨论】:

  • 你看到这个question了吗?
  • @Keo:谢谢。是的,这是一个类似的问题(使用不同的后端)
  • 只是好奇,这些错误是从哪里来的?我正在使用serializer.is_valid(raise_exception=True),但响应中缺少errors 字段。

标签: django ember.js ember-data django-rest-framework json-api


【解决方案1】:

您从后端收到的错误不是 JSON API 符合 errors
您必须在自定义序列化程序的 extractErrors 方法中转换错误(有关示例,请参阅 RESTSerializer 文档),或者您更改后端以返回 JSON API 符合错误。

multiple errors 的示例符合 JSON API 规范:

{
  "errors": [
    {
      "status": "403",
      "source": { "pointer": "/data/attributes/secret-powers" },
      "detail": "Editing secret powers is not authorized on Sundays."
    },
    {
      "status": "422",
      "source": { "pointer": "/data/attributes/volume" },
      "detail": "Volume does not, in fact, go to 11."
    },
    {
      "status": "500",
      "source": { "pointer": "/data/attributes/reputation" },
      "title": "The backend responded with an error",
      "detail": "Reputation service not responding after three requests."
    }
  ]
}

【讨论】:

【解决方案2】:

通过代码I found,django端需要这个设置:

REST_FRAMEWORK = {
    ...
    'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 2016-03-30
    • 2014-07-28
    相关资源
    最近更新 更多