[英]Rxjs 6: using catchError() gives You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable
I'm using the new syntax to comply with RxJS 6 pipe()
. 我正在使用新语法来遵守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. 同样使用Angular6。我有一个处理http请求的服务,它通过管道连接map和
catchError
在连接错误的情况下显示举杯。 Nevertheless, If I add catchError
I get in console You provided 'undefined' where a stream was expected. 但是,如果我添加
catchError
进入控制台, 您在期望流的位置提供了“ undefined”。 You can provide an Observable, Promise, Array, or Iterable. 您可以提供一个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}`);
})
);
}
If I remove the catchError
function the errors disappear in console, so that is the breaking piece of code. 如果我删除
catchError
函数,错误将在控制台中消失,因此这是断点代码。 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
的回调需要返回一个Observable(它可能会引发一个异常,我认为也会将其转换为error
)。
catchError((error):any => {
if (whatever) {
...
return empty(); // just complete
}
return throwError(`Connection Error: ${error}`); // return another `error`
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.