Javascript ngrx/将数据传递给操作员的效果
我有一个场景,当服务失败时,我必须获得传递的请求有效负载,这样我就可以返回错误响应。我的代码如下所示Javascript ngrx/将数据传递给操作员的效果,javascript,angular,ngrx,Javascript,Angular,Ngrx,我有一个场景,当服务失败时,我必须获得传递的请求有效负载,这样我就可以返回错误响应。我的代码如下所示 @Effect() doGetEvents$: Observable<Action> = this.actions$ .ofType(EVENTS) .switchMap((action) => { let eventDate = action.payload.date; return this.http.service(action.payload); }) .map
@Effect() doGetEvents$: Observable<Action> = this.actions$
.ofType(EVENTS)
.switchMap((action) => {
let eventDate = action.payload.date;
return this.http.service(action.payload);
})
.map(res => {
// success
if (res.status) {
return CustomActions.prototype.eventsResponse({ type: EVENTS_RESPONSE, payload: res.payload });
}
//failure
return CustomActions.prototype.EventsErrorResponse({
type: CustomActions.EVENTS_ERROR_RESPONSE,
payload: {
status: res.status,
errorMessage: res.errorMessage,
billDate: '10/01/2016', // <--- I need the eventDate got from the above switchMap
errorType: CustomActions.EVENTS + '_ERROR'
}
});
});
但这不会执行http
调用,也不会在.map()
参数上返回响应
还有一些选项可以使
eventDate
超出效果范围,并在服务失败时分配它,但这不是一种更干净的方法,应该有一些方法来传递数据,但不确定我错过了什么 如果希望将来自有效负载的信息与HTTP服务的结果一起包括在内,可以使用map
操作符,如下所示:
.switchMap((action) => {
return this.http
.service(action.payload)
.map(result => [action.payload.date, result]);
})
.map(([date, result]) => { ... })
如果您希望包括来自有效负载的信息以及HTTP服务的结果,可以使用
map
操作符,如下所示:
.switchMap((action) => {
return this.http
.service(action.payload)
.map(result => [action.payload.date, result]);
})
.map(([date, result]) => { ... })
这就是我所寻找的,我的坏,我没有通过“正常”的http服务方式思考,而是通过
可观察的方式:D这就是我所寻找的,我的坏,我没有通过“正常”的http服务方式思考,而是通过可观察的方式:D