RxJS SwitchMap Error when Piping HTTP Call
Solution 1:
error()
isn't a valid operator in RxJS to include inside pipe()
, instead you want to use catchError()
to handle specific error cases. However looking at your example, you don't need to use it since Angular will catch any errors thrown from your HTTP observable.
Additionally, you don't need to use switchMap because you're not relying on another observable. Just use the map()
operator and return result
instead of of(result)
.
login$(userRegistration: UserRegistrationRequest): Observable<AuthResult> {
return this.http.post<AuthResult>(url, userRegistration)
.pipe(
map(result => {
if(result.errors || result.errors.length !== 0) {
throw(result.errors);
}
// do things and then
return result;
})
);
}