David Papini
David Papini

Reputation: 21

angular ngrx store call multi http service into one effect and dispacth actions

Hi i need call multi http service in parallel mode, and to goal this i use forkJoin, but when it's finished it not dispatch the actsions.

doSearchCliente$ = createEffect(() =>
      this.actions$.pipe(
         ofType(addCliente),
         switchMap(action => {
            const cliente$ = this.clienteService.ClienteById(action.id).pipe(
               map(response => addClienteSuccess({ response })),
               catchError(() => of(addClienteFailure())));
            const listino$ = this.clienteService.ListinoCollection(action.id).pipe(
               map(response => addListinoSuccess({ response })),
               catchError(() => of(addListinoFailure())));

            return forkJoin([cliente$, listino$]).pipe(
               mergeMap(response => [response[0], response[1]])
            );
         }),
      ));

can someone help me?

Upvotes: 1

Views: 921

Answers (2)

Manuel Panizzo
Manuel Panizzo

Reputation: 896

try to add the first oeprator after each map of each http request. forkJoin emits when all Observables ended. and when you do a http request the observables technically do not end. So the first operator make those end.

doSearchCliente$ = createEffect(() =>
      this.actions$.pipe(
         ofType(addCliente),
         switchMap(action => {
            const cliente$ = this.clienteService.ClienteById(action.id).pipe(
               first(),
               map(response => addClienteSuccess({ response })),
               catchError(() => of(addClienteFailure())));
            const listino$ = this.clienteService.ListinoCollection(action.id).pipe(
               first(),
               map(response => addListinoSuccess({ response })),
               catchError(() => of(addListinoFailure())));

            return forkJoin([cliente$, listino$]).pipe(
               mergeMap(response => [response[0], response[1]])
            );
         }),
      ));

Upvotes: 2

David Papini
David Papini

Reputation: 21

do you say this?

doSearchCliente$ = createEffect(() =>
      this.actions$.pipe(
         ofType(addCliente),
         switchMap(action => {
            const cliente$ = this.clienteService.ClienteById(action.id).pipe(
               map(response => addClienteSuccess({ response })),
               catchError(() => of(addClienteFailure())));
            const listino$ = this.clienteService.ListinoCollection(action.id).pipe(
               map(response => addListinoSuccess({ response })),
               catchError(() => of(addListinoFailure())));

            return forkJoin([cliente$]);
         }),
         switchMap(response => {
            return [response[0]];
         })
      ));

not work! :(

Upvotes: 0

Related Questions