Reputation: 139
I am calling a rest API from angular, the rest api returns data in JSON Array of Objects like this
Now I am trying to convert it to my model class array but don't getting anywhere can you please guide me.
My Model Class
My Service File
Here it gives error on map and I don't know how to convert it to my model class array to display it in table
Upvotes: 0
Views: 1902
Reputation: 139
Resolved It.
As i was directly getting the array of objects in response, I don't need to convert it and use an interface. So here is my correct code
fetchAllGamesRecord() : Observable<Fifa[]>{
const fifaUrl = `${this.baseUrl}/fetchAllGamesRecord`;
return this.httpClient.get<Fifa[]>(fifaUrl);
}
This function is called like this
this.fifaService.fetchAllGamesRecord().subscribe(
data => {
this.allGameRecord = data;
console.log(`Data = `+data);
}
);
Upvotes: 1
Reputation: 3085
In the Spring framework also Fifa[] doesn't work in REST calls, we need to put it in another class as I showed below, I assume this will work.
export class FifaWrapper {
wrapper: Fifa[];
}
and also use this class in Observable
fetchAllGamesRecord(): Observable<FifaWrapper> {
... do you handling here
}
Upvotes: 0
Reputation: 2377
you can do that while calling fetchAllGamesRecord
fetchAllGamesRecord().subscribe( (response: Fifa[]) => {
// do something
}
where Fifa is interface not class
Upvotes: 0