[英]How do I pass two different pieces of state from my ngrx effect to my service function?
I have an effects function where I'm trying to pass data from my dispatched action as well as a separate selector to a function in a service, but I'm getting lost in the RXJS syntax.我有一个效果函数,我试图将数据从我的调度操作以及一个单独的选择器传递给服务中的函数,但我在 RXJS 语法中迷失了方向。 I'm pretty sure I'm not mapping the data correctly.
我很确定我没有正确映射数据。 Please see the relevant code.
请查看相关代码。
// effects // 效果
updateCohort$ = createEffect(() =>
this.actions$.pipe(
ofType(getCohortActions.updateActiveCohort.type),
withLatestFrom(this.store.pipe(select(activeCohort))),
map(([action, selector ]) => (
this.cohortFeaturesServices.updateCohorts(action, selector)
)),
// service // 服务
public updateCohorts(action, selector): Observable<any> {
const url = `api/services/cohorts/frontend/`;
return this.http.patch(`${url}/${selector.id}/`, action.changes);
}
Visual Studio Code underlines the entire function and I get the following error in my console. Visual Studio Code 强调了整个函数,我在控制台中收到以下错误。
Type 'Observable>' is not assignable to type 'Observable'.
类型 'Observable>' 不可分配给类型 'Observable'。 Property 'type' is missing in type 'Observable' but required in type 'Action'.
“Observable”类型中缺少属性“type”,但“Action”类型中需要。
How can I fix my effect and successfully pass my action and my selector to my service call?如何修复我的效果并将我的操作和我的选择器成功传递给我的服务调用?
You need to use switchMap/mergeMap/concatMap/exhaustMap
to "unbox" observable coming from HTTP service -> result of switchMap
-> map
into action such as your effect must return actions(such as it is just a default effect with action dispatch).您需要使用
switchMap/mergeMap/concatMap/exhaustMap
来“拆箱”来自 HTTP 服务的switchMap
-> switchMap
结果 -> map
到动作,例如您的效果必须返回动作(例如它只是带有动作调度的默认效果) .
Also, I suggest that the API method would have the changes as parameters, not an action.另外,我建议 API 方法将更改作为参数,而不是操作。
updateCohort$ = createEffect(() =>
this.actions$.pipe(
ofType(getCohortActions.updateActiveCohort.type),
withLatestFrom(this.store.pipe(select(activeCohort))),
switchMap(([action, cohort]) =>
this.cohortFeaturesServices.updateCohorts(action.changes, cohort)
),
map((result: CohortUpdateResponse) => successCohortUpdateAction())
)
)
public updateCohorts(changes: Partial<Cohort>, cohort: Cohort): Observable<CohortUpdateResponse> {
const url = `api/services/cohorts/frontend/`;
return this.http.patch(`${url}/${cohort.id}/`, changes);
}
Something like this.像这样的东西。
PS added some "invented" types to show what is going on and where PS 添加了一些“发明的”类型来显示正在发生的事情和位置
PSS didn't check for typos in code, such wrote answer right in answer window PSS 没有检查代码中的拼写错误,例如在答案窗口中写下答案
GL :) GL :)
For anyone else they may come across the same issue I figured it out.对于其他任何人,他们可能会遇到同样的问题,我想通了。 I wasn't returning an action.
我没有返回一个动作。
updateCohort$ = createEffect(() =>
this.actions$.pipe(
ofType(getCohortActions.updateActiveCohort.type),
withLatestFrom(this.store.pipe(select(activeCohort))),
exhaustMap(([action, cohort]) => this.cohortFeaturesServices.updateCohorts(action, cohort).pipe(
)),
map((response) => ({
type: getCohortActions.updateActiveCohortSuccess.type, // success action
success: response
})),
catchError((err) => of(getCohortActions.updateActiveCohortError(err))) // error action
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.