Home > Blockchain >  RxJS cacheError((error:any) cannot accept any object type as parameter
RxJS cacheError((error:any) cannot accept any object type as parameter

Time:10-21

I used RxJS like below:

  catchError((error: any) => {
    if (error instanceof HttpErrorResponse) {
      .......
      if (error.status === 401) {
          .....
          return;
        }}

But cacheError((error:any) usage gives below error:

Argument of type '(error: any) => Observable | undefined' is not assignable to parameter of type '(err: any, caught: Observable<HttpEvent>) => ObservableInput'. Type 'Observable | undefined' is not assignable to type 'ObservableInput'. Type 'undefined' is not assignable to type 'ObservableInput'

How can I solve it? I am using rxjs 6.6.3 version

CodePudding user response:

According to RxJS docs / learn

Remember to return an observable from the catchError function!

So to get rid of the error, you could return empty observable of():

catchError((error: any) => {
    if (error instanceof HttpErrorResponse) {
      .......
      if (error.status === 401) {
          .....
          return of();
        }}
  • Related