angular - Rxjs 6 : using catchError() gives You provided 'undefined' where a stream was expected. 您可以提供一个 Observable、Promise、Array 或 Iterable

标签 angular rxjs angular6 rxjs6

我正在使用新语法来符合 RxJS 6 pipe()。同样使用 Angular 6。我有一个处理 http 请求的服务,它通过管道传输 map 和 catchError 以在出现连接错误时显示 toast 。尽管如此,如果我添加 catchError 我会进入控制台 您在需要流的地方提供了“未定义”。您可以提供 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 的回调需要返回一个 Observable(我认为它可能会抛出一个将被转换为 error 的异常)。

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

关于angular - Rxjs 6 : using catchError() gives You provided 'undefined' where a stream was expected. 您可以提供一个 Observable、Promise、Array 或 Iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50972811/

相关文章:

javascript - 使用 Chrome 扩展内容脚本注入(inject) Angular 8 应用程序

angular - 执行 EventEmitter 的不同方法 - Angular

angular - 我如何在不使用共享模块的情况下定义将在 Angular 中的所有延迟加载模块中使用的全局 @pipe() ?

angular - 类型 'unsubscribe' 上不存在属性 'Observable<DataSnapshot>'

Angular RXJS forkJoin完成进度

Angular 6 router.events.filter 'filter' 在类型 'Observable<Event>' 上不存在

angular6 - Angular Material Design 上的分页 - 显示页码或删除行数

angular - 筛选对象属性匹配接口(interface)

angular - 在两个 Observable 完成后做一些事情

javascript - 更改按钮值 Angular 7