3

When the sent data is bad, it gets from the server tables with validator errors, as you can see in the picture. My question is how to get to this array so that you do not have to refer to each element individually on reasonably err.error.erros.email or err.error.erros.password. I would like to display these errors using sweetalert, one below the other.

My code:

 this.http.post('https://mylocalhost/api/1.3/user/login', params, {headers: config})
        .subscribe(res => {
                this.userData = res;
                swal('App', 'Zostałeś zalogowany pomyślnie', 'success');
                localStorage.setItem('x-ticket', this.userData['x-ticket']);
            },
            (err: HttpErrorResponse) => {
                console.log(err);
            });

Screenshot

6
  • which angular version are you using? Commented Aug 19, 2018 at 8:04
  • So you just need an easy way to get those errors? Commented Aug 19, 2018 at 8:06
  • @argo i use angular 6 Commented Aug 19, 2018 at 8:10
  • @Volodymyr Khmil yes, i need simple way. Although in time I would like to do, maybe some service that I will be hooking up and he will parse these errors. Commented Aug 19, 2018 at 8:11
  • Did you try catchError() from rxJs, and inside extract your errors learnrxjs.io/operators/error_handling/catch.html Commented Aug 19, 2018 at 8:16

2 Answers 2

2

You can use the following to convert it into an array

var error = {
  errors: {
     email: "This is an email error",
     password: "This is a password error"
  }
}


// Create an array from the object
let arr = Object.keys(error.errors).map((key) => error.errors[key]);

// Some examples of using the value
console.log(arr);
console.log(arr[0]);
console.log(arr.join("\n"));

Once you have an array, you can then display those however you wish without needing to refer to each error type.

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfect :D Thank for help
0

Below is one of the angular 5 code examples I'm using. You should be able to edit and and make it work in your case.

postNewInventoryEntry(data: any) {
    return this.http.post(this.api_url + '/inventory-management/inventory/', data)
      .map(res => res)
      .catch((e: any) => {
      alert('ERROR: ' + e.error['msg']);
      return _throw(e.error)});
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.