angular - 捕获并返回错误时,Rxjs 可观察到的抛出错误
问题描述
您好我收到此错误“您在预期流的位置提供了一个无效对象。当从我的效果调用此 http 请求时,您可以提供 Observable、Promise、Array 或 Iterable”。
deleteAccount(accountId: string): Observable<any> {
return this.http.delete(FUNDING_ACCOUNT_DELETE_URL
.replace(ACCOUNT_ID, accountId)).pipe(
timeout(2000),
catchError(err => {
return err; })
);
}
@Effect()
paymentOptionRemove = this.actions
.ofType(ActionTypes.PAYMENT_OPTION_REMOVE).pipe(
switchMap((action: PaymentOptionRemoveAction) =>
this.service.deleteAccount(action.payload).pipe(
map(
_ => new PaymentOptionRemovedAction()),
// tslint:disable-next-line:arrow-return-shorthand
catchError(err => {
if (err) {
console.log(err);
}
return of(new PaymentOptionRemoveErrorAction());
})
))
);
解决方案
从deleteAccount
函数中,您应该返回一个Observable
. 在catchError
块中,您试图返回err
,这是不可观察的。
使用throwError
来自rxjs
:
throwError
是一个来自 rxjs 的函数,它创建一个Observable
只向订阅者发出错误的函数。专为您的用例而设计。
import { throwError } from 'rxjs';
在删除函数中,使用如下:
return throwError(err);
推荐阅读
- oracle - Oracle 程序不能正常工作?
- html - 使用引导程序删除两列之间的空间
- google-sheets - 如何格式化 Google 表格持续时间格式的单元格以以人类可读的格式显示天数、小时数和分钟数?
- android - 使用菜单项在 SurfaceView 上绘制 SVG 图像
- javascript - 如何通过反应中兄弟组件上的单击事件将图像加载到另一个组件中
- python - 在第二个轴上选择 Numpy
- java - 我怎样才能做一个慢循环?
- java - FirebaseMessagingService onMessageReceived 方法未在后台调用 DATA 有效负载
- sql - 是/否 查询列的格式
- python-3.x - ExcelLibrary - 机器人框架骑行 - 导入问题