问题
This is my original effect. It loads data from a service call.
getReviewEffect$ = createEffect(() => this.actions$.pipe(
ofType(INIT),
mergeMap(({ Id }) => this.ReviewService.findByP(Id, new Date(new Date().setDate(new Date().getDate() -30)),
new Date(new Date().setDate(new Date().getDate() + 30)), 'Approval')
.pipe(
mergeMap(reviews => {
return [ReviewLoadSuccess({ drugReviews: getReviews(reviews) })
];
}),
catchError(error => {
return of(ReviewLoadFailure({ error: error }));
})
)
)));
But the id, I take form this service call in loading.
this.ocService.getActiveoc().subscribe((oc => {
this.Id = oc.id;
}));
I need to put the above code also, inside my effect and initial load it.need some expert help to do that. And also need to better way to given a date in 30 in the future and past.
--------------updated ------------------------------
I try to do it, like below but it was compiled error. need some expert help to resolve it,
getReviewEffect$ = createEffect(() => this.actions$.pipe(
ofType(INIT),
mergeMap((Id)=> this.socService.getActiveoc().toPromise().thenoc=>{return oc.id})
.then(((Id)=>{
this.drugReviewService.findBy(Id, new Date(),
new Date(new Date().setDate(new Date().getDate() + 1)), 'Approval')
.pipe(
mergeMap(reviews => {
return ReviewLoadSuccess({ Reviews: getReviews(reviews) })
];
}),
catchError(error => {
return of(ReviewLoadFailure({ error: error }));
})
)
})
)))
回答1:
You could try to use a Promise()
this.ocService.getActiveoc().toPromise()
.then(res => {this.Id = res['id']})
.then(() => {
getReviewEffect$ = createEffect(() => this.actions$.pipe(
ofType(INIT),
mergeMap(({ Id }) => this.ReviewService.findByP(Id, new Date(new
Date().setDate(new Date().getDate() -30)),
new Date(new Date().setDate(new Date().getDate() + 30)), 'Approval')
.pipe(
mergeMap(reviews => {
return [ReviewLoadSuccess({ drugReviews: getReviews(reviews) })
];
}),
catchError(error => {
return of(ReviewLoadFailure({ error: error }));
})
)
)));
})
来源:https://stackoverflow.com/questions/63626960/how-to-combine-this-two-sevice-call-with-in-the-single-effect-one-out-put-result