There is an error in the source code that leads to this error. You have three calls in your code, which I will call A), B) and C):
A) this.userService.getUserIdFromUserName(this.registerCredentials.username) // WORKS B) this.userService.getEmployeeIdFromUserId(localStorage.getItem("UserId")) // THIS RETURNS NULL C) this.authService.login(this.registerCredentials)
What you need to know about RXJS is the difference between a cold Observable (which represents all the information needed to start an async operation) and a hot Observable (which an Observable with an asynchronous operation is already running).
The three calls A), B) and C) simply build cold observables that start from the moment you call .subscribe() on them. Thus, at time point B), A) is already running, but not yet completed. Thus, calling localStorage.getItem("UserId") will return null since A) has not yet called the next caller back.
So what do you want to do for B) to wait for A). In addition, instead of stuffing something into a global state (localStorage), it is probably better to pass the result from A) to B). Enter .mergeMap() :
this.userService.getUserIdFromUserName(this.registerCredentials.username) // WORKS .map(res => res.toString()) .do(userId => localStorage.setItem("UserId", userId)) // cleanly separate side-effects into .do() calls .mergeMap(userId => this.userService.getEmployeeIdFromUserId(userId)) .map(res => res.toString()) .do(employeeId => localStorage.setItem("EmployeeId", employeeId)) .subscribe( employeeId => { console.log(employeeId); }, err => { console.log(err); });
The good thing about rxjs is that error handling works entirely through the Observable chain. If you can execute C) in parallel, see .forkJoin() .
Finally, if you need .mergeMap() explanations, look at this answer: SwitchMap vs MergeMap in the #ngrx example
Johannes Rudolph
source share