如何将这两个服务调用与一个单独的输出结果作为其他输入相结合?

时间:2020-08-28 03:17:37

标签: angular typescript ngrx ngrx-effects

这是我的原始效果。它从服务调用中加载数据。

   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 }));
        })
      )
    )));

但是ID,我是在加载时调用此服务调用的。

this.ocService.getActiveoc().subscribe((oc => {
  this.Id = oc.id;
}));

我还需要将上面的代码放入我的效果中并进行初始加载。需要一些专家的帮助。并且还需要一种更好的方式来指定未来30天的日期。

--------------更新------------------------------

我尝试这样做,如下所示,但它已编译错误。需要一些专家帮助来解决它,

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 个答案:

答案 0 :(得分:1)

您可以尝试使用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 }));
        })
      )
    )));
 })