Angular RxJS sequential requests issue

Ali Celebi

I have the following requests;

  public userLogin(username: string, password: string) {
    return this.http.post<any>(`${this.apiURL}/login `, {username, password}, constants.httpOptions)
      .pipe(
        map((response) => {
          this.setToken(response.token);
        })
      );
  }

  public getUser() {
    return this.http.get<IUser>(`${this.apiURL}/api/account`)
      .pipe(
        map((user: IUser) => {
          sessionStorage.setItem('user', JSON.stringify(user));
          this.userSubject.next(user);
        })
      );
  }

I would like to call them sequentially and check the returned value of the last request. I implemented the following block of code however, the console.log output is undefined within the subscribe section. In the network tab, I can see that both requests are called sequentially and there's no error.

this.authService.userLogin(username, password).pipe(
        concatMap(() => { return this.authService.getUser() })
      ).subscribe((res: any) => {
        console.log(res);
        this.router.navigate(['/home']);
      }, (err: HttpErrorResponse) => {
        if (err.status === 401) {
          console.log('sign-in page - 401');
          this.unauthorized = true;
        }
      });

What am I doing wrong?

Daniel Gimenez

This is an example of a good reason to specify return types in TypeScript. If you had you'd realize that the result of the map operator in getUser is always undefined. In this case I think you want to use tap which allows you to perform a side-effect while passing the source emission further through the stream.

public getUser(): Observable<IUser> {
  return this.http.get<IUser>(`${this.apiURL}/api/account`).pipe(
    tap((user: IUser) => {
      sessionStorage.setItem('user', JSON.stringify(user));
      this.userSubject.next(user);
    })
  );
}

You probably want to use tap in setToken as well if you intend on returning the token value from that method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Make concurrent requests using rxjs mergeMap() is sequential

Angular 2 - Sequential HTTP requests

Angular rxjs sets of sequential http calls

RxJS batch HTTP requests in Angular

how to optimize sequential processing of network requests between two servers in rxjs

Two or more sequential requests inside another (Angular)

Making two http requests sequentially in angular, rxjs

Angular RxJS Nested Subscribe with multiple Http Requests

Angular rxjs call http requests in batches with a delay

Angular and RxJs combine two http requests

angular rxjs: handle multiple http requests

Handling two HTTP requests (Angular + RxJS)

Angular5 RXJS recursive http requests

How to call the API in sequential order in Angular9 with rxjs?

Multiple sequential API calls using RXJS in Angular 12 app

How to manage multiple sequential subscribe methods in Angular and RxJs?

Angular RxJs - sequential HttpRequests with passing previous response data to next request

Sequential API call using Rxjs in Angular with null return

Converting rest api promises requests to rxjs requests in angular ionic

How to send sequential requests with comlex logic in send function in Angular

Angular 5 RxJS Subscription Timing Issue

RxJS fromEvent target type issue (Angular 13)

Using RXJS to issue two sequential http calls and returning the result of the first one only

Forced sequential requests with AngularJS

Sequential axios requests in a map

Sequential execution of Ajax requests

Two sequential HTTP requests

Angular - RxJs ForkJoin How To Continue Multiple Requests Even After A Error

Angular Testing - Chained HTTP Requests using rxjs switchMap