1

I have a problem with a variable or something related to timing. I am iterating over a forEach loop and for each element I check if its mail is true through a function of a module. If this email is true I put it in a new empty array. The problem is that the array is filled with that data but only within the loop. Once out the loop is still empty, does anyone know why?

The module:

-npm install email-existence

And now the problem:

var validos = [];    
sinRepetidos.forEach((element) => {
      emailExistence.check(element.Email, function (error, response) {
        var exist = false;
        // If the email is false print the email and remove from the list.
        if (response) {
          console.log(
            element.Nombre + " con email " + element.Email + " es: " + response
          );
          exist = true;
        }
        if (error) {
          console.log(error);
        }
        if (exist) {
          validos.push(element);
        }
      });
    });

Array with some data:

    [
  {
    Id: '3',
    Nombre: 'Dato3',
    Apellidos: 'Prueba Test',
    Email: 'dato3@email.com'
  },
  {
    Id: '5',
    Nombre: 'Dato5',
    Apellidos: 'Prueba Test',
    Email: 'dato5@email.com'
  },
  {
    Id: '2',
    Nombre: 'Dato2',
    Apellidos: 'Prueba Test',
    Email: 'dato2@email.com'
  },
  {
    Id: '4',
    Nombre: 'Dato4',
    Apellidos: 'Prueba Test',
    Email: 'dato4@email.com'
  }
]
garikoitzz
  • 23
  • 5
  • Not sure how much this will help, but here's fixed code: https://jsfiddle.net/18zrm6j5/ –  Mar 31 '21 at 10:24

0 Answers0