I have encountered an issue with my form submission process involving two functions. When both functions succeed, I want to display a successful validation message. However, currently the success message is displayed even if the second function fails. How can I ensure that both functions are successful before showing the validation message?
First Function
registerUser(user: cusInfo) {
let userinfo = { cusInfo: { ...user } }
this.registrationService.saveUserInfo(userinfo).subscribe(data => {
// handle success
},
error => {
// handle error
});
}
Second function
registerInfo({ code,name }) {
let item = { "reserve": { code,name} };
console.log(item);
this.registrationService.infoRequest(item).subscribe(data => {
// handle success
},
error => {
// handle error
});
}
Form submit
registerCustomer(item, info, reservelockerform: NgForm) {
this.alertService.clear();
this.registrationService.checkDuplicateUser(info.userName).subscribe(data => {
if (data.executionDescription == 'Success') {
this.registerUser(info); // call first function
this.registerInfo(item); // call second function
this.alertService.success('Registration has been made successfully');
} else {
this.alertService.error(data.executionDescription);
}
});
}
In order to solve the issue of displaying the validation message correctly, I need to modify my form submit function to only show it when both functions have succeeded.