我正在使用新语法来符合 RxJS 6 pipe()
。同样使用 Angular 6。我有一个处理 http 请求的服务,它通过管道传输 map 和 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
函数,控制台中的错误就会消失,所以这就是中断代码。关于可能出错的任何想法?
最佳答案
catchError
的回调需要返回一个 Observable(我认为它可能会抛出一个将被转换为 error
的异常)。
catchError((error):any => {
if (whatever) {
...
return empty(); // just complete
}
return throwError(`Connection Error: ${error}`); // return another `error`
});
关于angular - Rxjs 6 : using catchError() gives You provided 'undefined' where a stream was expected. 您可以提供一个 Observable、Promise、Array 或 Iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50972811/