0

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!

Vitalizzare
  • 4,496
  • 7
  • 13
  • 32
  • You are not returning `probar` to the function `gestionMailContactos`. – Get Off My Lawn Aug 13 '22 at 19:18
  • Your return statement is inside the `then` function, so your function actually does not return anything. pull it outside by using `await` like this: `const result = await probar`, and then you can return what you want according to `hayMail` variable – Eli Porush Aug 13 '22 at 19:22

0 Answers0