Reputation: 1006
I'm looking for the best way to handle an observable in Angular. The current design is:
public login(email: string, password: string): Observable<string> {
return this.http
.post(environment.apiUrl + '/login', {
email: email,
password: password
})
.map(response => {
this.authenticationService.setToken(response["token"]);
return "OK;"
})
.catch(error => {
console.error('LoginService::error', error);
return throwError(error);
});
}
This all works fine but I don't want to be returning "OK" for no reason.
I tried the following but it says you can't assign Observable < void> to Observable< never>
public login(email: string, password: string): Observable<never> {
return this.http
.post(environment.apiUrl + '/login', {
email: email,
password: password
})
.map(response => {
this.authenticationService.setToken(response["token"]);
})
.catch(error => {
console.error('LoginService::error', error);
return throwError(error);
});
}
Upvotes: 0
Views: 788
Reputation: 1170
public login(email: string, password: string) {
return this.http
.post(environment.apiUrl + '/login', {
email: email,
password: password
}).pipe(
tap({
next: response => {
this.authenticationService.setToken(response["token"]);
},
error: error => {
console.error('LoginService::error', error);
},
}),
)
}
The tap operator adds some logic to your Observable for every emission and for the error/complete events if you so desire. The operator doesn't expect any return value and ignores them altogether, and everything else happening with the Observable will continue as usual.
Upvotes: 1