I'm working on getting a json object array using an http get request. Currently, I have a list-view.component that calls a function, getEvents() in my event.service component. That function returns Promise, which represents the object array. It successfully returns the updated array to list-view.component, but for some reason, when I try to print out the array on console, it doesn't show the changes.
list-view.component.ts
this.eventService.getEvents(lat, lon, range, s).then(events => this.eventsList = events);
console.log(this.eventsList);
event.service.ts
getEvents(lat: number, lon: number, radius: number, s: string): Promise<Event[]> {
return this.http.get(this.eventsUrl + "?s=" + s +"&radius=" + radius + "&lat=" + lat + "&lon=" + lon)
.toPromise()
.then(response => response.json() as Event[])
.catch(this.handleError);
}
If I try to print the array in the .then method, like below, it prints it correctly.
this.eventService.getEvents(lat, lon, range, s).then(events => console.log(events));
How come, when I try to print it after the this.eventService.getEvents() call, it doesn't have the new changes?