N
N
Nikolay Semenov2018-08-15 15:08:28
JavaScript
Nikolay Semenov, 2018-08-15 15:08:28

How to get data in mergeMap?

Guys hello everyone. Tell me how to get a token in mergeMap

@Effect()
  authSignin = this.actions$
    .ofType(AuthActions.TRY_SIGNIN)
    .pipe(
      map((action: AuthActions.TrySignin) => {
        return action.payload;
      }),
      switchMap((authData: { email: string; password: string }) => {
        console.log(authData)
        return this.httpClient.post('http://143.167.112.46:3001/sessions/create', authData)
      }),
      switchMap((token) => {
        console.log(token)
        const headers = new HttpHeaders().set(
          'Authorization',
          'bearer ' + token.id_token
        );
        return this.httpClient.get('http://143.167.112.46:3001/api/protected/user-info', {headers})
      }),
      mergeMap((token: string, user) => {
        console.log(user, token)
        this.router.navigate(['/']);
        return [
          {
            type: AuthActions.SIGNIN
          },
          {
            type: AuthActions.SET_TOKEN,
            payload: token
          }
        ];
      });
    );

only user from the last switchMap comes to mergeMap

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Luzanov, 2018-08-15
@nickola105

Well, you can do something like that, if you really want to.

switchMap((token) => {
        console.log(token)
        const headers = new HttpHeaders().set(
          'Authorization',
          'bearer ' + token.id_token
        );
        return this.httpClient.get('http://143.167.112.46:3001/api/protected/user-info', {headers}).pipe(
          map(user => {
            return { user, token }
          })
        )
      }),
      mergeMap({ user, token }) => {
        console.log(user, token)
        this.router.navigate(['/']);
        return [
          {
            type: AuthActions.SIGNIN
          },
          {
            type: AuthActions.SET_TOKEN,
            payload: token
          }
        ];
      });

But I would take
this.httpClient.get('http://143.167.112.46:3001/api/protected/user-info', {headers})
and all other requests to the server in the service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question