Rxjs 6:使用 catchError() 为您提供 'undefined' 预期流的位置。您可以提供 Observable、Promise、Array 或 Iterable

Rxjs 6: using catchError() gives You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

我正在使用新语法来符合 RxJS 6 pipe()。还使用 Angular 6. 我有一个处理 http 请求的服务,它通过管道传输 map 和 catchError 以在出现连接错误时显示祝酒词。尽管如此,如果我添加 catchError 我会进入控制台 你提供了 'undefined' 预期流的位置。您可以提供 Observable、Promise、Array 或 Iterable。

  getDataHttpRequest(url, returnType) {
    return this.http.get(url, this.getRequestOptions())
    .pipe(
      map((response) => {

        if(response){

        if (returnType === 'object') {
          return response[0] == undefined ? response : response[0];
        } else {
          return response;
        }

      }
      }),
      catchError((error):any => {

      if(error instanceof HttpErrorResponse && error.status === 0){
        //no connection error(either user has no connection or the server is down)
       this.toastr.error('Chequee su conexión e intente de nuevo en unos momentos. Contáctenos para reportar el problema a <a href="mailto:dev@info.com">dev@info.com</a>',"Error de Conexión",{tapToDismiss:true, disableTimeOut: true});
      }
       throwError(`Connection Error: ${error}`);
      })


    );

  }

如果我删除 catchError 函数,控制台中的错误就会消失,所以这就是中断代码。关于可能出错的任何想法?

catchError 的回调需要 return 一个 Observable(我认为它可能会抛出一个将被转换为 error 的异常)。

catchError((error):any => {
  if (whatever) {
    ...
    return empty(); // just complete
  }
  return throwError(`Connection Error: ${error}`); // return another `error`
});