Get the request action parameter value after switchMap action is completed

Viewed 377

I have a simple effect which calls an API to get data and after I get the data, I dispatch an action to save the data in store. I would like to do another action or operation based on the action request. Is that possible?

  loadMoviesById$ = createEffect(() => this.actions$.pipe(
    ofType('[Movies Page] Load Movies'),
    mergeMap((action) => this.moviesService.getAll(action.id)
      .pipe(
        map(movies => ({ type: '[Movies API] Movies Loaded Success', payload: movies })),
        catchError(() => EMPTY)
      )),
     tap((action) => {
            console.log(action.id) //How can I get action.id without passing with loaded success
        })
    )
  );

I am looking to pass the action. id to the tap parameter which is a request parameter for the movies

1 Answers

Since it's outside of the scope where the action's id is known, you can't access it. As a fix, you have two options:

  • move the tap method inside the mergeMap scope
  • add the id to the Movies Loaded Success action
Related