mafortis
mafortis

Reputation: 7138

loop errors in angular

In angular I've tying to loop my given errors and show them in alert but it doesn't work, I've read and tried different solutions such as using lenght or lenght+1 etc. none of them seem to work.

Data

{name: Array(1), email: Array(1), password: Array(1)}
email: ["The email field is required."]
name: ["The name field is required."]
password: ["The password field is required."]
__proto__: Object

Code

error => {
        let errors = error.error.errors;
        console.log(errors); //return result above
        for (var i = 0; i < errors.length; i++) {
          this.alertService.presentToast(errors[i]);
        }
      },

any idea?

Upvotes: 1

Views: 1325

Answers (2)

Ravi Heer
Ravi Heer

Reputation: 103

You can simply use this

Object.keys(errors).forEach(key => {
    alert(errors[key][0]);
});

Upvotes: 2

Ravinder Kumar
Ravinder Kumar

Reputation: 120

Hi there are two ways to print the error messages. Hope it may help full for you

1.

for (let [key, value] of Object.entries(errors)) {
    console.log(value[0]);
}

2.

for (let [key, value] of Object.entries(errors)) {
    for(let msg of value) {
        console.log(msg);
    }   
}

Upvotes: 4

Related Questions