Using http.get().subscribe the data returns as a empty array, however if I call a console.log() inside the subscribe code the array shows correctly.
This is the code:
public data: Noticia[] = [];
public api = "http://localhost:3000/data"
constructor(
private httpClient: HttpClient
) { }
ngOnInit() {
this.httpClient.get(this.api).subscribe((response: Noticia[]) =>{
response.forEach(news =>{
this.data.push(news);
});
console.log("data in subscribe", this.data);
});
console.log("data",this.data);
console.log("data [0]",this.data[0]);
}
This is the console logs:
How can I keep the data after the subscribe is over?