【问题标题】:How can I display the errors from an POST Response in the UI?如何在 UI 中显示来自 POST 响应的错误?
【发布时间】:2017-09-12 16:43:06
【问题描述】:

如果登录无效,则会收到以下响应。

Response {
  _body: "{
      "email": ["The email field is required."],
      "password":["The password field is required."]
     }",
  status: 422, ok: false, statusText: "Unprocessable Entity", headers: Headers
}

我想在 UI 中向用户显示错误。这是我对 observable 的实现。

Class

export class LoginPage
{
  signInData: any[];
  errors:     any[];
  ...

  this.userService.signIn(form.value.email, form.value.password)
  .subscribe(
    data => this.signedInData = data,
    error => this.errors = error._body
   );
}

我只是将检索到的 _body 对象设置为 this.errors。如果我尝试如下显示错误,

Template

 <ul *ngFor="let error of errors">           
   <li>{{error}}</li>
 </ul>

我收到以下错误。

./LoginPage 类 LoginPage 中的错误 - 内联模板:30:19 导致:找不到不同的支持对象 '{"email":["The email field is required."],"password":["The密码字段是必需的。"]}' 类型为 'string'。 NgFor 只支持绑定到 Arrays 等 Iterables。

我该如何处理?

UPDATE

如果我将订阅更改为:

this.userService.signIn(form.value.email, form.value.password)
  .subscribe(
    data => this.signedInData = data,
    error => this.errors.push(error._body)
);

<ul *ngFor="let error of errors">           
   <li>{{error}}</li>
</ul>

它成功打印了错误对象。现在我需要访问对象中的值,删除大括号并打印值。怎么样?

WORKING CODE

感谢@echonax,我能够将数据解析为 JSON,检查它是否存在,然后将其一一推送到错误数组中。

this.userService.signIn(form.value.email, form.value.password)
.subscribe(
  data => this.signedInData = data,
  error =>
  {
        let err = JSON.parse(error._body);
        if(err && err.email && err.email[0]){
          this.errors.push(err.email[0]);
        }
        if(err && err.password && err.password[0]){
          this.errors.push(err.password[0]);
        }  
  }
);

<ul *ngFor="let error of errors">           
   <li>{{error}}</li>
</ul>

【问题讨论】:

    标签: json angular


    【解决方案1】:

    你指的是错误的变量

    <ul *ngFor="let error of error">           
       <li>{{error.email}}</li>
     </ul>
    

    您将错误存储在errors 变量中,并且引用了signInErrors

    注意:您应该将其显示为 json,因为您无法预测服务会出现什么错误。

    <span>{{error |json }}</span>
    

    更新 1: 您应该转换为 json() 如下

    this.userService.signIn(form.value.email, form.value.password)
      .subscribe(
        data => this.signedInData = data,
        error => this.errors = JSON.parse(error._body)
       );
    

    【讨论】:

    • 这是一个错字,问题仍然存在。我尝试按照您的建议添加 json 管道,但使用转义引号时出现相同的错误。 ION: Error in ./LoginPage class LoginPage - inline template:30:17 caused by: Cannot find a differ supporting object '"{\"email\":[\"The email field is required.\"],\"password\":[\"The password field is required.\"]}"' of type 'string'. NgFor only supports binding to Iterables such as Arrays.
    • 感谢您的宝贵时间。看起来那一定是我错过的。但现在它产生了类似的错误:EXCEPTION: Error in ./LoginPage class LoginPage - inline template:30:17 caused by: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
    • 您需要更多帮助还是问题已解决?
    • 是否有返回任何错误的特定格式。我的意思是export interface errorClass 我们可以使用通用的吗?
    • 那么问题是否已解决或需要帮助?
    【解决方案2】:

    *ngFor 仅支持 Arrays 等可迭代对象

    您的error.body 是一个对象

    "{
        "email": ["The email field is required."],
        "password":["The password field is required."]
     }"
    

    所以不兼容*ngFor

    而不是使用

    <ul *ngFor="let error of errors">           
       <li>{{error}}</li>
    </ul>
    

    试试:

    <span>{{errors | json}}</span>
    

    就像@Aravind 提到的那样

    如果你想单独选择它们,这样的事情可能会起作用:

    <span>{{errors?.email[0]}}</span>
    <span>{{errors?.password[0]}}</span>
    

    更新

    如果

    this.errors.push(error._body) 有效

    为了单独获取值;试试:

    this.userService.signIn(form.value.email, form.value.password)
      .subscribe(
        (data) => this.signedInData = data,
        (error) => {
                     error = JSON.parse(error._body);
                     if(error._body && error._body.email && error._body.email[0]){
                       this.errors.push(error._body.email[0]);
                     }
                     if(error._body && error._body.password && error._body.password[0]){
                       this.errors.push(error._body.password[0]);
                     }                     
                   }
    );
    
    <ul *ngFor="let error of errors">           
       <li>{{error}}</li>
    </ul>
    

    【讨论】:

    • 我知道我哪里出错了。 {{ errors | json }} 会打印整个对象。是的,我需要单独打印它们。如果我为同样的错误执行{{ signInErrors?.email[0] }},我会得到Error in ./LoginPage class LoginPage - inline template:31:23 caused by: Cannot read property '0' of undefined - 尽管电子邮件字段仍然是空的。
    • 对于{{errors?.email}},它似乎什么也没打印。另外,对象中可能有也可能没有email 属性。
    • @anonym 如果它没有该属性,它当然不会打印它。但是它确实并且不打印它那么我们有一个问题
    • 没错,但它不是打印它。因此,如果我使用 console.log(this.errors),我会得到以下信息。 ---Array[1] 0 : "{"email":["The email field is required."],"password":["The password field is required."]}" length: 1 __proto__:Array[0]。 errors 是一个包含单个对象的数组。
    • 这似乎是正确的,应该就是这样。但它仍然输出Cannot read property '0' of undefined
    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多