Home > Software engineering >  In angular 12 I need to call an api which will give token and then I need to pass that token to anot
In angular 12 I need to call an api which will give token and then I need to pass that token to anot

Time:08-01

Below is my code having two methods getToken and validateuser, I am taking the token from getToken and passing it to the parameter of validateuser. But before getting the token my second api calls is getting executed.

 ngOnInit(): void {
           this.login()
          }
getToken(){
    const authParam = {
      username: "abc",
      password: "abc"
    };
    this.apiService.getToken(authParam)
      .subscribe(
        response => {
          console.log("here1");
          this.token = response;  
          console.log(this.token);
        },
        error => {
          console.log(error);
        });
  }

    vlidateUser(){
    this.getToken();
    const loginParam ={
      userId:"abc",
      serviceType:"UV",
      auth_token:this.token
    }
    this.apiService.validateUser(loginParam)
    .subscribe(
      response => {
        console.log("here2");
        console.log(response);
      },
      error => {
        console.log(error);
      });
  }

  login(){
      this.vlidateUser();
  }
  
}

CodePudding user response:

Use async await

    ngOnInit(): void {
           this.login()
          }
getToken(){
    const authParam = {
      username: "abc",
      password: "abc"
    };
    this.apiService.getToken(authParam)
      .subscribe(
        response => {
          console.log("here1");
          this.token = response;  
          console.log(this.token);
        },
        error => {
          console.log(error);
        });
  }

    async vlidateUser(){
    await this.getToken();
    const loginParam ={
      userId:"abc",
      serviceType:"UV",
      auth_token:this.token
    }
    this.apiService.validateUser(loginParam)
    .subscribe(
      response => {
        console.log("here2");
        console.log(response);
      },
      error => {
        console.log(error);
      });
  }

  login(){
      this.vlidateUser();
  }
  
}

CodePudding user response:

  • Use async before vlidateUser()
  • Use await before this.getToken()
 ngOnInit(): void {
   this.login()
 }
 getToken(){
     const authParam = {
       username: "abc",
       password: "abc"
     };
     this.apiService.getToken(authParam)
       .subscribe(
         response => {
            console.log("here1");
            this.token = response;  
            console.log(this.token);
         },
         error => {
          console.log(error);
         });
  }

  async vlidateUser(){
      await this.getToken();
      const loginParam ={
         userId:"abc",
         serviceType:"UV",
         auth_token:this.token
      }
      this.apiService.validateUser(loginParam)
        .subscribe(
          response => {
          console.log("here2");
          console.log(response);
        },
        error => {
          console.log(error);
        });
  }

  login(){
      this.vlidateUser();
  }
  
}
  • Related