I tried to send an HttpRequest in Typescript. I need to store the recieved data outside of the subscribe function. But when I want to print the user variable outside of the subscribe, it is undefined
.
In the subscribe function it works fine. So how I can get access to the data outside of the subcribe function?
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);
}