I have the following code snippet:
login() {
const body = JSON.stringify({
"usuario":"juanma",
"password":"1234"});
console.log(body);
let tokencito:string = ''
const params = new HttpParams();
const headers = new HttpHeaders();
this.http.post<AuthResponse>("http://localhost:8000/login", body, {'headers':headers,'params':params})
.subscribe(data =>
// @ts-ignore
this.tokencito = data.token.token, localStorage.setItem('token', this.tokencito));
console.log(localStorage.getItem('token'))
}
I am using this code to retrieve a token from a user in PHP. However, it does not wait for the data to be retrieved. When I execute it for the first time, it returns 'data' as undefined. But when I run it a second time, it retrieves the token correctly.
An image has been included below for better clarification:
https://i.sstatic.net/RVsOW.png
Is there a way to make it wait for the post method to finish before proceeding?