angularrxjsngrxngrx-effects

Looping through an array of object based on ID in ngrx effects


I want to loop through the ids, present in the array of objects and then pass it to another action. I tried data.foreach and I get an error. Most probably I am not using the correct RXjs operator. Any help in this would be highly appreciated.

Example data= [{id:1, name: 'name1'}, {id:2, name: 'name1'}]

loadSucces$ = createEffect(() =>
            this.actions$.pipe(
                ofType(Actions.loadSuccess),
                switchMap(([data]) =>
// data returns an array of objects, from whose id I want to go through
                    this.service
                        .get('data.id')
                        .pipe(
                            map(serviceData =>
                                Actions.loadDataSuccess({ serviceData })
                            ),
                            catchError(error =>
                                of(Actions.loadDataSuccess({ error }))
                            )
                        )



                )
            )
        );

Solution

  • https://timdeschryver.dev/blog/multiple-service-calls-from-an-effect

    refresh$ = createEffect(() =>
      this.actions$.pipe(
        ofType(CustomerActions.refresh),
        exhaustMap(({ customerIds }) =>
          merge(
            ...ids.map(id =>
              this.customersService.getCustomer(id).pipe(
                map(CustomerActions.getCustomerSuccess),
                catchError(err =>
                  of(CustomerActions.getCustomerFailed(id, err.message)),
                ),
              ),
            ),
          ),
        ),
      ),
    )