I want to retrieve my travel details, which are returned from an API in Angular.
From the backend, I receive:
{
"trips": [
{
"id": 0,
"name": "string",
"startDate": "2019-06-30T06:05:48.006Z",
"endDate": "2019-06-30T06:05:48.006Z",
"description": "string",
"roomSharing": true,
"countries": [
{
"id": 0,
"name": "string",
"code": "string"
}
],
"languages": [
{
"id": 0,
"name": "string",
"code": "string"
}
]
}
]
}
This is all good, but I'm facing a challenge on the client side. Here's my code for fetching trips:
getTrips(): Observable<Trip[]> {
return this.http.get<Trip[]>(this.apiUrl + '/Trip/Get')
.pipe(
tap(_ => console.log('fetched trips')),
retry(1),
catchError(this.handleError),
map(data => {
return data;
})
);
}
In my component, I have:
loadTrips() {
return this.rest.getTrips()
.subscribe((data) => {
this.trips = data;
console.log(this.trips);
}
, err => console.log(err));
}
I want to display trips in a template like:
<div class="card mb-3 trip" *ngFor="let trip of trips">
But currently, I need to use:
<div class="card mb-3 trip" *ngFor="let trip of trips.trips">
Hence, my question is how can I transform my response to get an array of Trip instead of an array of Trips arrays?