0

I am currently watchin a tutorial, and I'm really keeping an eye on what's going on. However, I am constantly getting this error:

ERROR TypeError: Cannot read property 'forEach' of undefined

This is my part of code where I get that:

onSubmit(){
this.service.register().subscribe(
  (res: any) => {
    if (res.succeeded){
      this.service.formModel.reset();
      this.toastr.success('Korisnik kreiran!', 'Registracija uspješna!');
    } else {
      res.errors.forEach((element:any)=>{
        switch (element.code){
          case 'DuplicateUserName':
            this.toastr.error('Korisničko ime se već koristi.', 'Neuspjela registracija.');
          break;
            //Registration failed
          default:
            this.toastr.error(element.description, 'Neuspjela registracija.');
          break;
        }
      });
    }
  },
  err=>{
    console.log(err);
  }
);
}

Does anybody has idea what's going on? Thanks.

2
  • 3
    It means there is no errors property in your response. Commented Apr 27, 2021 at 16:45
  • Your tutorial seems to be out of sync with the project's angular version. Commented Apr 27, 2021 at 16:51

1 Answer 1

1
onSubmit(){
this.service.register().subscribe(
  (res: any) => {
    if (res.succeeded){
      this.service.formModel.reset();
      this.toastr.success('Korisnik kreiran!', 'Registracija uspješna!');
    } else {
    if ('errors' in res) {
      res.errors.forEach((element:any)=>{
        switch (element.code){
          case 'DuplicateUserName':
            this.toastr.error('Korisničko ime se već koristi.', 'Neuspjela registracija.');
          break;
            //Registration failed
          default:
            this.toastr.error(element.description, 'Neuspjela registracija.');
          break;
        }
      });
     }
    }
  },
  err=>{
    console.log(err);
  }
);
}

Adding an if block before you loop through errors should resolve the issue. Still let me know if you are still facing any issue.

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

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.