profiler
profiler

Reputation: 627

NGRX return array in effect

When I try to run below code:

functionMap = {
[ActionTypeEnum.TYPE_FIRST.toLowerCase()]:[getInitialDataStatus({statusType: 'disabled'}),fetchImportantDataFirst()],
[ActionTypeEnum.TYPE_SECOND.toLowerCase()]:[getInitialDataStatus({}),
                        fetchImportantDataSecond()],
[ActionTypeEnum.TYPE_THIRD.toLowerCase()]:[getInitialDataStatus({}),
                        fetchImportantDataThidr()]
}

 startMultpileActionDepensOnType$ = createEffect(() => this.actions$.pipe(
        ofType(startMultpileActionDepensOnType),
        withLatestFrom(this.store$.select(selectTypeActions)),
        map(([action, select]) => {
                const actionType = select.type.toLowerCase();
                return this.unctionMap[actionType]

            }
        )
    ));

I got the error about:

TS2322: Type 'Observable<TypedAction<string>[]>' is not assignable to type 'Observable<Action>'. Property 'type' is missing in TypedAction<string>[].

But when I change to: functionMap:{},

I got the error:

Action must have a type property

Upvotes: 0

Views: 1225

Answers (1)

Thierry Falvo
Thierry Falvo

Reputation: 6290

Inside an effect, to retrieve multiple actions, so an array of actions, you have to use switchMap operator :

load$ = createEffect(() => this.actions$.pipe(
  ofType(actions.load),
  switchMap(action => [
    actions.loadItems(),
    actions.loadHistory();
  ])
))

So in your case, functionMap returns an array of actions :

startMultipleActionDepensOnType$ = createEffect(() => this.actions$.pipe(
  ofType(startMultipleActionDepensOnType),
  withLatestFrom(this.store$.select(selectTypeActions)),
  switchMap(([action, select]) => {
    const actionType = select.type.toLowerCase();
    return this.functionMap[actionType];
  })
));

Upvotes: 1

Related Questions