gpt4 book ai didi

javascript - 在 Angular 中使用 ngrx 时捕获超时错误

转载 作者:行者123 更新时间:2023-12-02 21:58:20 25 4
gpt4 key购买 nike

我想在effect发出Http请求时处理超时错误。

这是我的效果

loadSchedulings$ = createEffect(() =>
this.actions$.pipe(
ofType(ESchedulesActions.GetSchedulesByDate),
mergeMap(() =>
this.apiCallsService.getSchedulings().pipe(
map(trips => ({ type: ESchedulesActions.GetSchedulesByDateSuccess, payload: trips })),
catchError(err => {
console.log(err);
return EMPTY;
})
)
)
)
);

这是我的getSchedulingService

getSchedulings() {
return this.http.get<ISchedules>(this.urlData).pipe(
map(data => {
...
return groupByDate;
}),
timeout(2500),
catchError(error => of(`Request timed out`))
);
}

实际上,我的effect内的catchError没有捕获任何错误,但是我在我的服务函数内发送了错误。

如何使用 rxjs 运算符来做到这一点?

最佳答案

我的看法是,您将在 getSchedulings() 调用中捕获错误,并且您永远不会传播此错误,而是返回它的新可观察值。尝试用 throwError 替换 of :

    catchError(error => throwError(`Request timed out`))

然后将由您的效果处理

关于javascript - 在 Angular 中使用 ngrx 时捕获超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59946096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com