I'm trying to assign the returned array from the server in withing the subscribe method. The problem I'm facing is that after I assign the array it shows as undefined if I console log it. However, if I console log it inside the subscription method it shows all elements.
Is there something that I'm missing?
Here is my code:
export class GetMoviesComponent implements OnInit {
movies: Movie[];
constructor(private movieService: MovieServiceService) {
}
ngOnInit() {
this.movieService.getMovies().subscribe((data: Movie[])
=> {
this.movies = data;
});
this.lol();
}
lol() {
console.log(this.movies);
}
}
And below is my service that calls the API
getMovies(): Observable<Movie[]> {
return this.http.get<Movie[]>(this.baseUrl + 'getmovies');
}