Rxjs 6:使用catchError()可以为您提供“未定义”的预期流。您可以提供一个Observable,Promise,Array或Iterable

Jua*_*uan 3 rxjs angular angular6 rxjs6

我正在使用新语法来遵守RxJS 6 pipe()。同样使用Angular6。我有一个处理http请求的服务,它通过管道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}`);
      })


    );

  }
Run Code Online (Sandbox Code Playgroud)

如果我删除该catchError功能,错误将在控制台中消失,因此这是断点代码。有什么想法可能有问题吗?

mar*_*tin 6

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

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