Rxjs 6:使用 catchError() 为您提供了“未定义",其中需要流.你可以提供一个 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

查看:26
本文介绍了Rxjs 6:使用 catchError() 为您提供了“未定义",其中需要流.你可以提供一个 Observable、Promise、Array 或 Iterable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新语法来符合 RxJS 6 pipe().也使用 Angular 6.我有一个处理 http 请求的服务,它通过管道映射和 catchError 显示连接错误时的吐司.尽管如此,如果我添加 catchError 我会进入控制台 您提供了未定义",其中需要一个流.您可以提供 Observable、Promise、Array 或 Iterable.

I'm using the new syntax to comply with RxJS 6 pipe(). Also using Angular 6. I have a service that handles http requests, and it pipes map and catchError to display a toast in case of connection error. Nevertheless, If I add catchError I get in console You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or 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 函数,控制台中的错误就会消失,所以这是破坏性的代码片段.关于可能出什么问题的任何想法?

If I remove the catchError function the errors disappear in console, so that is the breaking piece of code. Any ideas on what could be wrong?

推荐答案

catchError 的回调需要返回一个 Observable(它可能会抛出一个将转换为 error 以及我认为).

The callback for catchError needs to return an Observable (it might throw an exception that will be converted to error as well I think).

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

这篇关于Rxjs 6:使用 catchError() 为您提供了“未定义",其中需要流.你可以提供一个 Observable、Promise、Array 或 Iterable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆