CONTEXT
An Angular 8 application requests for an external web api.
The GET request result is something like that:
{
"data": [
{
"name": "QWERTY",
"isValid": false
}]
}
I am calling the api with this TypeScript code in my service:
private categoryUrl = 'http://localhost:8081/api/categories?OrderBy=';
return this.http.get<ICategory[]>(this.categoryUrl + prop )
.pipe(
tap(data => console.log(JSON.stringify(data)))
,map(item=>item.data)
);
}
MY PROBLEM
This line of code doesn't compile claiming:
error TS2339: Property 'data' does not exist on type 'ICategory[]'
If I comment the map() line and run it's compiling and the web application starts, but no data are displayed on the html template.
If then I uncomment without stopping, the application recompile, send the same error message. But the request is done correctly and I can see my result on the template.
MY QUESTIONS
- How I can explain this?
- What is the correct way to do the job?
Thank you
item[0].dataor turn array into single ICategory - do the last option if the Get result is actually like you have shown in first json.