When attempting to send an HttpRequest in Typescript, I encountered an issue where the received data could not be stored outside of the subscribe function. Despite successfully saving the data within the subscribe block and being able to access it there, when trying to print the 'user' variable outside of the subscribe function, it appears as undefined
. How can I retrieve the data from the subscription to use it outside of this scope?
private user: User;
public sendHttpLogin(username: string, password: string) {
this.http.get<User>('http://localhost:8080/login?password='+password+'&username='+username).subscribe(data => {
//save the data on a User Object
this.user = data;
//this works fine
console.log(this.user)
});
//this is undefined
console.log(this.user);
}