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

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

问题描述

我正在使用新语法来遵守RxJS 6 pipe().同样使用Angular6.我有一个处理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的回调需要返回一个Observable(它可能会抛出异常,并且我认为也会将其转换为error).

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

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}`);
      })


    );

  }

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?

解决方案

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天全站免登陆