I have been reviewing all the answers about this question but I really do not understand how to do it. What I am trying to do is, before I save the info of the form, to check if at least one contact has an email. I have tried everything (obviously with no success) I show you my code. This first part is in my save form function:
const comprobar = await this.gestionMailContactos(altaRaw['contactos']);
comprobar.then(() => {
if (!comprobar) { return false; }
});
And the function I call is:
async gestionMailContactos (contacto): Promise<any> {
let hayMail = false ;
let probar = new Promise((resolve, reject) => {
contacto.forEach((element, index) => {
this.formularioService.getContacto(element)
.subscribe( (data: any) => {
if (data.email !== null) { hayMail = true; resolve(hayMail); }
if (index === contacto.length - 1) {resolve(hayMail); }
});
});
});
probar.then(() => {
if (!hayMail ) {
Swal.fire({
title: 'Alta Inmueble',
text: 'Al menos un contacto debe tener email',
icon: 'warning'
});
return false;
} else {
return true;
}
});
comprobar
always returns undefined. Any help appreciated!