I am faced with a situation where I need to make 2 dependent API calls: the getCars call requires the user id obtained from getUser.
There is a possibility that a user may not have any cars, resulting in a 404 error from the API.
How can I handle this scenario by returning the aggregated response, and when no cars are returned, simply using null/undefined for userData.cars?
I attempted to use catchError in the pipeline of getCars, but it doesn't seem to be effective:
getUser().pipe(
switchMap(user => user.creditScore > 70 ?
getCars(user.id).pipe(
catchError(() => of(undefined)),
map( cars => ({user, cars}) )
)
: of({user: user, cars: undefined)
)
).subscribe((userData) => {
if (userData.user) {
........
}
if (!!userData.cars) {
.........
}
},
err => {
......
});