Currently I am making 3 different API calls which are below like this:-
//API Call1
getUserById(id){
return this.http
.get(`${environment.userAPI}/user/${Id}`, { headers: this.headers })
.pipe(
catchError(err => {
return throwError(this.errorHandler.handleError(err));
}),
);
}
//API Call 2:-
getTeamById(id){
return this.http
.get(`${environment.TEAM_API}/team/${Id}`, { headers: this.headers })
.pipe(
catchError(err => {
return throwError(this.errorHandler.handleError(err));
}),
);
};
////API Call 3:-
getGroupById(id){
return this.http
.get(`${environment.GROUP_API}/group/${Id}`, { headers: this.headers })
.pipe(
catchError(err => {
return throwError(this.errorHandler.handleError(err));
}),
);
};
//Now I am making all the Three API calls at once using forkJoin in the below way:-
forkJoin([getUserById(1), getTeamById(1),getGroupById(1)]).pipe(
catchError(this.getCatchError)
)
.subscribe([res1,res2,res3]=>{
console.log(res1,res2,res3)
})
Now I am having a requirment where using res1 i need to check one condition and make another API call and combine with the above 3 results and my condition is on API call 1 when we get user details i need to check one field team-leader which contains one Id and based on that Id i need to make another API call which is like below:-
////API Call 3:-
getLeaderById(id){
return this.http
.get(`${environment.Leader_API}/leader/${Id}`, { headers: this.headers })
.pipe(
catchError(err => {
return throwError(this.errorHandler.handleError(err));
}),
);
};
I am achieving the above requirement in the below way:-
forkJoin([getUserById(1), getTeamById(1),getGroupById(1)]).pipe(
catchError(this.getCatchError)
)
.pipe(
mergeMap(res =>
if(res[0]?.teamLeaderId){
return getLeaderById(res[0]?.teamLeaderId).//Here I am getting res as undefined
} else{
return res;
}
)
)
.subscribe([res1,res2,res3,res4]=>{
console.log(res1,res2,res3,res4);//I am never ever reaching this line of code
})
Is there any mistake I am doing? By the end when all API calls are done I need to have 3 results as mandatory and 4th one is optional based on condition. I am new to RXJS. Note:- I am using the above code in Angular9 using typescrip