I'm using Angular 8.2 and calling a .NET Core 3.1 API and I'm able to get the call to work and return the data that I need, but the syntax I ended up using looks like it could be simplified, but so far no alternatives that I've tried seem to work.
I'm calling the API this way:
getCatsApi(): Observable<any[] | DataServiceError> {
console.log("API");
let mcats = this._http.get<any[]>( Settings.apiRoot + 'api/Cat/GetCats')
.pipe(
catchError(err => this.handleHttpError(err))
)
console.log("Get Cats from API");
console.log(mcats);
return mcats;
}
This returns a response structure that looks like this:
The data I want is in the "returnedEntity" property, just below the "responseErrors" property.
I'm able to get at this data in what seems like a messy way:
getCats(){
this._dataService.getCatsApi().subscribe(data => {
for(let j = 0; j < data['returnedEntity']['catList'].length; j++) {
console.log(data['returnedEntity']['catList'][j]);
this.catsObj.push(data['returnedEntity']['catList'][j]);
}
});
}
This does work and I receive the data that I need. But it seems like there must be a better way to get the returnedEntity back without explicitly referring to it with array strings. I am using <any[]> in the original HTTP call, but if I change that to the <Category[]> interface that I created it doesn't seem to make a difference. I'm guessing that I need to use some form of the rxjs .map() function, but so far I haven't found a way to make that work. This makes me wonder if something else is wrong? Any thoughts would be greatly appreciated! Thank you!
