One challenge I encountered in my Angular 12 application is capturing the response of an API call made in the service file within my component. The issue arises from the asynchronous nature of the API response, which causes the console to always display 'undefined'. Despite attempting to use async await, I have yet to find a solution:
WITHIN THE SERVICE FILE:
public checkEmail(emailToVerify: any) {
this.myService.emailValidate({ email: emailToVerify }).subscribe({
next: (data: { result: any) => {
console.log('updateEmail-res', data);
return data;
},
error: (err: any) => {
console.log('updateEmail-err', err);
}
});
}
WITHIN THE COMPONENT:
this.apiResponse = await this.myService.checkEmail(customerEmail);
console.log("this.apiResponse", this.apiResponse)