1

How do I map the user_id part of my response out to the user_id variable? My response is: {"token":"1a68322c4cad219c94d9554e30e4c8a00339e428","user_id":23,"username":"test5","first_name":"test5","email":"[email protected]"}

I can get the first console.log(this.user_id) to show the user_id but the second one does not work. I want to use it to redirect me to another page..

  login(user){ 
    this.userApiService.login({'username': this.user.username, 'password': this.user.password}).subscribe( resp => {
      let response = resp;
      this.user_id = response.user_id
      console.log(this.user_id)
      });
      console.log(this.user_id)
      this.router.navigateByUrl('/dashboard/'+this.user_id)
  }

4 Answers 4

3

this.userApiService.login is asynchronously so you don't know when this.user_id = response.user_id is executed. You should use this.router.navigateByUrl inside subscribe.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, i didnt realize i could have a redirect in the login function..
3

The problem is that the code for the second console.log and your router navigation is outside the subscribe block. You need to move your routing up inside the subscribe, like this:

login(user){ 
    this.userApiService.login({'username': this.user.username, 'password': this.user.password}).subscribe( resp => {
      let response = resp;
      this.user_id = response.user_id;
      console.log(this.user_id);
      this.router.navigateByUrl('/dashboard/'+this.user_id);
    });
  }

The reason why your initial code does not work is that the code inside your "subscribe" will run asyncronously, meaning that it will happen after your api has returned with the answer. So what happens in your initial code is this:

  1. The userApiService is called and it sends an http request to your server.
  2. The code after the subscribe block is run, meaning your second console.log and router navigation. At this point in time the http call has not returned with the user_id, so the code does not know what the user_id is.
  3. The response from your http request comes back, and the code inside your subscribe block is now run. You now have access to the user_id.

Comments

0

That happens that way because, it's navigating to dashboard even before you get response. It's not waiting till response comes. Keep .navigateByUrl() inside .subscribe (). So, it'll navigate after getting response

Comments

0

Because userApiService.login method return observable object.

What is observable ?

Observable is:

Observables provide support for passing messages between publishers and subscribers in your application. Observables offer significant benefits over other techniques for event handling, asynchronous programming, and handling multiple values. For more example please visit this link: Angular Observables

in a nutshell userApiService.login's return type is observable, so that the it is working async, therefore your code is not correct working. Please below code part move to subscribe's inside.

console.log(this.user_id)
this.router.navigateByUrl('/dashboard/'+this.user_id)

For example :

login(user){ 
    this.userApiService.login({'username': this.user.username, 'password': this.user.password}).subscribe( resp => {
      let response = resp;
      this.user_id = response.user_id
      console.log(this.user_id)

      //it is correct 
      console.log(this.user_id)
      this.router.navigateByUrl('/dashboard/'+this.user_id)
      });
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.