I am trying to fetch data from an API that looks like this:
The data is succesfully fetched and I can log it on my console like this:
component.ts:
getRacvaData(id:number){
this.racvaStat=[];
this.service.GetRacvaPodaci(id).subscribe(data => {console.warn(data)});
}
service.ts
GetRacvaPodaci(racvaId: number)
{
return this.http.get('http://11.112.3.160:8888/Racve/api/v1/getRacva?racva_id='+racvaId);
}
But, when I try to put it in a variable and, for example, log that variable or smth, it doesnt give any result. I put the data in a variable like this:
public racvaStat:any = [];
getRacvaData(id:number){
this.racvaStat=[];
this.service.GetRacvaPodaci(id).subscribe(data => this.racvaStat = data);
for(let item of this.racvaStat){
console.log(item.nit_a); // this is not logging anything
}
}
Does anybody know what could be the problem here? I want to put it in the array so I can use that array all over my project.


this.service.GetRacvaPodaci(id).subscribe((data:any) => { this.racvaStat = data; console.log(this.racvaStat) for(let item of data){ console.log(item.nit_a); } });this.racvaStat, as long as you use this value after your service has completed.