Felix
Felix

Reputation: 83

TypeError: Cannot read property 'emailTaken' of null

When I enter my email in my RegisterForm and it's equal to the one that sets ['emailTaken'] to true. I get the following error code:

ERROR TypeError: Cannot read property 'emailTaken' of null

Here is the code of my function:

checkEmail(control:FormControl): Promise<any> | Observable<any>{
    const answer = new Promise<any>((resolve, reject) => {
      setTimeout(() => {
        if(control.value == '[email protected]'){
          resolve({'emailTaken': true})
        }else{
          resolve(null)
        }
      },1500);
    });
    return answer;
  }

And here I trigger the ERROR in HTML:

<span *ngIf="registerForm.get('email').errors['emailTaken']" class="help-block">
                            Email is already taken!
                        </span>

How can I fix this so it doesn't give me errors?

Upvotes: 0

Views: 57

Answers (2)

Amit Chigadani
Amit Chigadani

Reputation: 29715

hasError can be more useful for checking the errors.

<span *ngIf="registerForm.get('email').hasError('emailTaken')" class="help-block">
       Email is already taken!
</span>

Upvotes: 1

Hien Nguyen
Hien Nguyen

Reputation: 18975

You can check null by ? operator

<span *ngIf="registerForm.get('email')?.errors['emailTaken']" class="help-block">
                            Email is already taken!
                        </span>

Upvotes: 1

Related Questions