We have a task where we need to make multiple REST API calls from the ngOnInit() method, one after the other. After making the first call, we need to pass the response to the second API call, and similarly for the third call, we need to get the value from the second call.
However, when we try calling them like this, we always end up with an undefined value.
this.globalVar1 : any;
this.globalVar2 : any;
this.globalVar3 : any;
async ngOnInit() {
this.apiService.getFirstAPICall(request).subscribe(info => {
this.globalVar1 = info; //the value is coming from the API service call here
}
console.log(this.globalVar1); //this logs as undefined
//Now we are trying to call the second API **here we need the first variable
//but since it's undefined, we are encountering an error
this.apiService.getSecondAPICall(request.globalVar1).subscribe(info => {
this.globalVar2 = info;
}
console.log(this.globalVar2); //this also shows undefined