Currently following an instructional guide on Angular 7 and have a question regarding the correct method to retrieve a list of items if they are nested rather than at the top level:
List at Top Level (working)
[
{ id: 123, name: 'Name 123'},
{ id: 124, name: 'Name 124'},
// ...
]
Nested List (not working)
{
message: 'x items found.',
data: [
{ id: 123, name: 'Name 123'},
{ id: 124, name: 'Name 124'},
// ...
]
}
Service Integration
// ...
@Injectable({ providedIn: 'root' })
export class ItemService {
private apiUrl = 'api/items';
constructor(private http: HttpClient) {}
getItems (): Observable<Item[]> {
// Facing an issue here:
return this.http.get<Item[]>(this.apiUrl, {
pathToItems: 'data'
});
}
}
Component Implementation
// ...
this.itemService.getItems()
.subscribe(items => this.items = items);
One approach could be to utilize subscribe()
in the service and extract the contents of .data
, but I believe there might be a simpler solution available. The objective is to maintain the return type as an array of Item
.
Any suggestions or insights would be greatly appreciated.
Thank you!