How to combine this two sevice call with in the single effect one out put result as input of other?

社会主义新天地 提交于 2021-01-29 07:32:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!