Error not displaying when returning from api

1251 views angular
0

I have a login service -

attemptAuth(type, credentials): Observable<User> {
const route = (type === 'login') ? 'login' : 'register';
return this.apiService.post(route, credentials)
  .pipe(map(
    data => {
    this.setAuth(data);
    return data;
    }
));
}

Here is the component part -

this.userService
  .attemptAuth(this.authType, credentials)
  .subscribe(
    data => {
        this.router.navigateByUrl('/')
    },
    err => {
      this.errors = err.Message;
      this.isSubmitting = false;
    }
  );

Data is getting set to null but error is not getting displayed.

answered question

1 Answer

6

If data is null, it will call subscribe success callback, which will perform redirect. If you want to trigger error callback.

Service:

attemptAuth(type, credentials): Observable<User> {
const route = (type === 'login') ? 'login' : 'register';
return this.apiService.post(route, credentials)
  .pipe(map(
    data => {
      if(data){
        this.setAuth(data);
        return data;
      }else{
        throw new Error('data api error'));
        return;
      }
    }
));
}

posted this

Have an answer?

JD

Please login first before posting an answer.