How do I do the following with lettable operators and pipe?
this.httpClient
.get(url)
.map((res: any) => {
const events = res.eventList;
return events.map(e => new EventLogModel(e));
})
.catch(this.handleError);
I've tried this, but I can't get catchError
to work: catchError does not exist on type Observable<any>
:
this.httpClient
.get(url)
.pipe(
map((res: any) => {
const events = res.eventList;
return events.map(e => new EventLogModel(e));
})
)
.catchError(this.handleError);
Also, I assume catch
and catchError
are the same, correct? I'm importing it like so:
import { map, catchError } from 'rxjs/operators';
but I wasn't sure if this was the correct operator to use.
Catch errors in the observable stream Another option to catch errors is to use the CatchError Operator. The CatchError Operators catches the error in the observable stream as and when the error happens. This allows us to retry the failed observable or use a replacement observable.
You'll need to use retry or retryWhen (names are pretty self-explanatory) — these operators will retry a failed subscription (resubscribe to the source observable, once an error is emitted.
The pipe method is for chaining observable operators, and the subscribe is for activating the observable and listening for emitted values. The pipe method was added to allow webpack to drop unused operators from the final JavaScript bundle. It makes it easier to build smaller files.
Your assumption is correct, the lettable operator catchError
is the same as catch
.
As for the placement of catchError
, it should not have prefix .
and should be placed within pipe
:
this.httpClient
.get(url)
.pipe(
map((res: any) => {
const events = res.eventList;
return events.map(e => new EventLogModel(e));
}),
catchError(this.handleError);
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With