I have one service that returns a list of vehicles. I need to get the driver for each vehicle and set it for the bus. So far, I have implemented it in such a way:
return this.getVehicles().pipe(
// Set the driver
map((res) => {
res.items = res.items.map((vehicle) => {
if (vehicle.driver_id !== "") {
this.driverSvc.get(vehicle.driver_id).subscribe((data) => {
vehicle.driver = data;
})
}
return vehicle;
})
return res;
}),
map((res) => {
// This map needs to have the driver!
console.log(vehicle.driver) // Undefined in all cases, where it should be set for one of the entries
})
I know the console.log(vehicle.driver) logs undefined because of the subscribe. Is there any other way to modify my response based on the response from another service?
switchMapoperator to combine two observables to a observable chain.