Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function return not being printed in console

I have this piece of code I'm writing in node.js, and I have a problem.

async function CheckearWhitelist(id) {
  const oracheck = ora("Checkeando en la whitelist...").start();
  con.connect(function (err) {
    if (err) {
      oracheck.fail(
        "Ha habido un fallo al conectarse a MySQL - Comprueba la conexión"
      );
    }
    con.query(
      `SELECT * FROM \`whatsapp\`.\`whitelist\` WHERE \`contacto\` = '${id}'`,
      function (err, result, fields) {
        if (err) {
          oracheck.fail("Fallo de MySQL");
        } else if (result == []) {
          oracheck.warn("Usuario no encontrado en la whitelist");
          return false
        } else {
          date = new Date();
          horas_fecha = date.getHours();
          minutos = date.getMinutes();
          hora = `${horas_fecha}.${minutos}`;

          hora_desb = result[0].hora_desbloqueo;
          if (hora_desb == 0) {
            oracheck.info("El contacto está en la whitelist indefinidamente");
            return true
          } else if (hora_desb <= hora) {
            oracheck.warn("Usuario no encontrado en la whitelist");
            return false
          } else {
            oracheck.succeed("Resultado Encontrado: ");
            return true
          }
        }
      }
    );
  });
}

CheckearWhitelist("[email protected]").then((value) => console.log(value));

The thing is that if I make console.log(true/false) and not the return, it perfectly works. But when I return true/false, and make a console.log when calling the function, it doesn't work. I hope someone can help me. Thanks

like image 803
Alvaro Artano Avatar asked Nov 20 '25 04:11

Alvaro Artano


1 Answers

The issue is that any return value from the asynchronous function you have there won't be returned back as you expect (it will be discarded or be used by the lib - but not your code)

You should instead return new Promise object at top level and resolve it inside the callback when you want to "return" values.

function CheckearWhitelist(id) {
  return new Promise((resolve, reject) => {
    const oracheck = ora("Checkeando en la whitelist...").start();
    con.connect(function(err) {
      if (err) {
        oracheck.fail(
          "Ha habido un fallo al conectarse a MySQL - Comprueba la conexión"
        );
      }
      con.query(
        `SELECT * FROM \`whatsapp\`.\`whitelist\` WHERE \`contacto\` = '${id}'`,
        function(err, result, fields) {
          if (err) {
            oracheck.fail("Fallo de MySQL");
          } else if (result == []) {
            oracheck.warn("Usuario no encontrado en la whitelist");
            resolve(false)
          } else {
            date = new Date();
            horas_fecha = date.getHours();
            minutos = date.getMinutes();
            hora = `${horas_fecha}.${minutos}`;

            hora_desb = result[0].hora_desbloqueo;
            if (hora_desb == 0) {
              oracheck.info("El contacto está en la whitelist indefinidamente");
              resolve(true)
            } else if (hora_desb <= hora) {
              oracheck.warn("Usuario no encontrado en la whitelist");
              resolve(false)
            } else {
              oracheck.succeed("Resultado Encontrado: ");
              resolve(true)
            }
          }
        }
      );
    });
  })
}

CheckearWhitelist("[email protected]").then((value) => console.log(value));
like image 90
Tibebes. M Avatar answered Nov 21 '25 18:11

Tibebes. M