I have a variable that i have instantiated in the top of my typescript class like this:
UserTimeRegistration: TimeRegistration[];
I have a method calling an api like this:
getUserInfo(UserID: string): Observable<TimeRegistration[]> {
return this.http
.get<TimeRegistration[]>(this.apiURL + '/TimeRegistrations/' + UserID)
.pipe(retry(1), catchError(this.handleError));
}
and I subscribe to it like this:
this.getUserInfo(this.smartTimeUser.UserID).subscribe((data: TimeRegistration[]) => {this.UserTimeRegistration = data;})
If I look at my variable inside the subscribe, i can see the value is assigned, but as soon as i move outside, its empty. I am probably missing something basic, but I have played with it for an hour without getting the point.. So I hope there is an expert here that shakes his head, rips out his hair and laugh at my stupidity before getting to his/her keyboard and type what I am doing wrong, how i should do it and why ;-)
I have tried multible variations, but i cant seem to figure out why it cant keep a variable after i leave subscribe.
subscribe is asynchronous
Please refer to this question for more understanding it already has a clear explantion...
I think this might explain your doubt.. if not I need more details like where in the function the data from response is not accessible.. (code wise)