Petr
Petr

Reputation: 99

How propagate action.payload from Action in ngrx-effect to catchError or map operator?

I write ngrx effect with httpRequest which use action.payload of Action.

From request I get data which I use in map operator (if error occur i handle it in catchError)

From Backend I get only boolean value, How I can propagate action.payload from Action to map or catchError operator?

Example:

effect= this.actions$
    .ofType(Actions.IS_OK)
    .pipe(
         mergeMap(action => this.service.isOk(action.payload),
         map((isOk: boolean)  => new SuccessAction(isOk, action.payload), //here I need somehow get to action.payload value
         catchError(error => of(new ErrorAction(error, action.payload))) //here I need somehow get to action.payload value
     );

And I can not store payload to store, because I have triggered more actions: Actions.IS_OK

Thanks for help

Upvotes: 4

Views: 1025

Answers (2)

Tobias Lindgren
Tobias Lindgren

Reputation: 306

Using some newer rxjs/ngrx syntax this can be written as:


isOkEffect$ = createEffect(() => this.actions$.pipe(
  ofType(Actions.IS_OK),
  mergeMap(({payload}) => this.service.isOk(payload).pipe(
    map((isOk: boolean) => new SuccessAction(isOk, payload),
    catchError(error => of(new ErrorAction(error, payload)))
  ))
));

Upvotes: 2

Pierre Mallet
Pierre Mallet

Reputation: 7221

Can you try :

effect = this.actions$
    .ofType(Actions.IS_OK)
    .pipe(
         mergeMap(action => this.service.isOk(action.payload).pipe(
             map((isOk: boolean) => { payload: action.payload, isOk: isOk} )
         ),
         // need to create proper interface forPayload + isOk 
         map((payloadPlusIsOk: any)  => new SuccessAction(payloadPlusIsOk.isOk, payloadPlusIsOk.payload),
         catchError(error => of(new ErrorAction(error, action.payload))) //here I need somehow get to action.payload value
     );

Upvotes: 1

Related Questions