I have a service with such a HTTP request: `
login(email:any,password:any){
this.http.post<{token:string}>('http://localhost:3000/login',{payload:{email,password}},{
headers:new HttpHeaders({'Content-Type':'application/json'})
}).pipe().subscribe(res=>{this.token=res.token})
}
a variavle
private token!: string;
and a helper function:
getToken() {
console.log(this.token)
return this.token;
}
`
The problem is that when the first time I call getToken() I get an Undefined and only if I call it one more time I get a valid value.
async/await
mechanism. Please make sure that the login function and API call has to be completed before making a call togetToken
function. There is a chance of delay since it's an API call.