Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
return returns an undefind value

im having issues with return in my function, i got two functions, the first one creates a random series of letters and numbers and the other one puts a few combinations together into a key and checks the database for it (to check if it already exists and if it does then it tries again), at the end of the second function im returning the key value as im trying to use it in my post request code. here is the code:

function keyGen(length) {
    var result           = '';
    var characters       = '0123ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789456789';
    var charactersLength = characters.length;
    for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
   }
   return result;
}

function makeKey(company) {
    var gen1 = keyGen(6);
    var gen2 = keyGen(4);
    var gen3 = keyGen(6);

    var key = `${company}-${gen1}-${gen2}-${gen3}`;
    var rawKey = `${gen1}-${gen2}-${gen3}`;
    con.query('SELECT * FROM lkeys WHERE chars = ?', [rawKey], function(err, rows, fields) {
        console.log("here")
        if (err) throw err
        if (rows.length > 0) {
            makeKey(company)
        }
        else {
            console.log("here2")
            console.log(key)
            return key;
        }
    })
}

this is how i try to use the value from the function:

let lkey = makeKey(company);

out put for lkey is undefined

any help would be appreciated, thank you.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda