1
I consume an API that returns a JSON to me. The return works perfectly, however, when I try to access some property of this JSON, it gives me as Undefined. I already JSON and is ok
JSON:
[
{
"ratingDate": "2018-12-21",
"rate1": 0,
"rate2": 0,
"rate3": 1,
"rate4": 0,
"rate5": 0,
"detractor": 100,
"promoter": 0,
"nps": -100,
"Reserva": 1,
"Abertura": 0,
"Finalizar": 0,
"VeiculoSujo": 0,
"Abastecimento": 0,
"CheiroCigarro": 0,
"VagaRuim": 1,
"Outro": 0
},
{
"ratingDate": "2018-12-22",
"rate1": 1,
"rate2": 0,
"rate3": 0,
"rate4": 0,
"rate5": 4,
"detractor": 20,
"promoter": 80,
"nps": 60,
"Reserva": 0,
"Abertura": 0,
"Finalizar": 0,
"VeiculoSujo": 1,
"Abastecimento": 1,
"CheiroCigarro": 1,
"VagaRuim": 0,
"Outro": 0
},
{
"ratingDate": "2018-12-23",
"rate1": 0,
"rate2": 0,
"rate3": 0,
"rate4": 1,
"rate5": 8,
"detractor": 0,
"promoter": 89,
"nps": 89,
"Reserva": 0,
"Abertura": 0,
"Finalizar": 0,
"VeiculoSujo": 1,
"Abastecimento": 0,
"CheiroCigarro": 0,
"VagaRuim": 0,
"Outro": 0
}
]
In my service, I have simple method for this call:
getNPS(){
return this.http.get(API);
}
In my component is also very simple:
public npsArray: any[]
constructor(private npsService: NpsService) {
}
ngOnInit() {
this.getNPS();
}
getNPS(){
this.npsService.getNPS().subscribe((data: any[]) => {
this.npsArray = data;
console.log(this.npsArray);
})
}
}
In the console.log shows me the right JSON, but if I try a property, for example this.npsArray.Vagamau, it gives me as Undefined
It is an array of objects. Try
this.npsArray[0].VagaRuim
.– Francisco
It worked, but how would I go through this object in an ngFor for example?
– Daniel Swater