Api response (json) -
{
"count": 1050,
"next": "....",
"previous": null,
"results": [
{
"name": "Test1",
"url": "https://test/1/"
},
{
"name": "Test2",
"url": "https://test/2/"
}
]
}
Interface:
export interface ITestModel{
name: string;
url: string;
}
Service:
getData(): Observable<ITestModel[]> {
return this.http.get<ITestModel[]>(this.apiUrl).pipe(
map((res) =>
res.map((data) => ({
name: data.name,
url: data.url
}))
)
);
}
Error: (When I subscribe the above function in controller)
core.js:4352 ERROR TypeError: res.map is not a function at MapSubscriber.project (test.service.ts:19)
Expectation:
- In getData() function, I would like to transform observable as ITestModel[]
- Expected output while subscribe (Json)
[ { "name": "Test1", "url": "https://test/1/" }, { "name": "Test2", "url": "https://test/2/" } ]
Thank you !
Try this: