arj
arj

Reputation: 983

Unable to call an action from effect

Am new in angular and ngrx store concept(ngrx and angular version is 13).

am trying to call an action form my createEffect method in effects.ts file

createEffect method look like this

loadData$ = createEffect(() => this.actions$.pipe(
      ofType('[Data] Load data'),
      mergeMap(() => this.myService.getDataApi()
        .pipe(
          map((data: Data[]) => ({      
            type: '[Data] Load Success',
            payload:  { data},
            selectedData:data[0],

            return setData(selectedData),
            }
          )),
          catchError((error: any) => of({
            type: '[Data] Load Failure',
            payload:  { error },
            }
          ))
        ))
    )
  );

Here am calling an API called getDataApi. Once i received the data from back end,setting payod and type.Finally am calling setDataand pass the fist value from response.

SetData action

export const setData = createAction(
    '[Data] Set Selected',
      props<{  selectedData: Data}>(),
  );

error

enter image description here

Thanks in advance.

Upvotes: 1

Views: 528

Answers (1)

Anton Marinenko
Anton Marinenko

Reputation: 3002

What's up with your code? This is a wrong part obviously:

map((data: Data[]) => ({      
            type: '[Data] Load Success',
            payload:  { data},
            selectedData:data[0],

            return setData(selectedData),
            }
          )),

Change with:

switchMap((data: Data[]) => setData({ selectedData: data[0] })),

Upvotes: 3

Related Questions