【问题标题】:Rxjs 6: using catchError() gives You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or IterableRxjs 6:使用 catchError() 为您提供了预期流的“未定义”。您可以提供 Observable、Promise、Array 或 Iterable
【发布时间】:2018-06-21 15:52:58
【问题描述】:

我正在使用新语法来遵守 RxJS 6 pipe()。同样使用Angular 6。我有一个处理http请求的服务,它通过管道映射和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 函数,错误会在控制台中消失,因此这是一段代码中断。关于可能出错的任何想法?

【问题讨论】:

    标签: angular rxjs angular6 rxjs6


    【解决方案1】:

    catchError 的回调需要返回一个 Observable(我认为它可能会抛出一个异常,该异常也会转换为 error)。

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

    【讨论】:

    • 谢谢马丁。非常感谢您的帮助。我急于截止日期,我忘了把退货xD
    猜你喜欢
    • 2020-03-26
    • 2021-12-10
    • 2018-09-07
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多