如何在NGRX Effects中正确发送多个HTTP请求?

时间:2018-11-12 11:33:26

标签: javascript rxjs ngrx ngrx-effects

我为单个HTTP请求编写了以下Effect: ( getRequestObj 的有效载荷是一个对象)

@Effect()
   indexDoc$ = this.actions$.pipe(
      ofType<IndexDocuments>(myActionTypes2.IndexDoc),
      mergeMapTo(this.store.select(getRequestObj).pipe(
         take(1)
      )),
      mergeMap((request: any) => this.myService.indexDoc(request).pipe(
         map((response: any[]) => new IndexSuccess(response)),
         catchError(error => of(new IndexFail(error)))
      ))
   );

我想要的是,用数组提供效果,并为数组中的每个项目发送HTTP请求。我试过的 ( getRequestArray 的有效载荷是对象数组)

@Effect()
   indexDocs$ = this.actions$.pipe(
      ofType<indexDocs>(myActionTypes1.IndexDocs),
      concatMapTo(this.store.select(getRequestsArray)),
      concatMap((request: any[]) => {
         return zip(...request.map(item => {
            return this.myService.indexDocs(item).pipe(
               map(response => {
                  return this.store.dispatch(new IndexSuccess(response))
               }),
               catchError(error => {
                  return Observable.of(this.store.dispatch(new IndexFail(error)));
               })
            )
         }))
      })

发生的是,所有请求都返回412 Precondition Failed错误。我如何使其正常工作?

0 个答案:

没有答案