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

125
Vistas
cannot access result set outside the function using mysql in nodejs

I cannot access the result set outside its scope.

function getAllProducts() {
   connection.connect((err) => {
      if (err) throw err
      connection.query(
         'SELECT * FROM `men`', (err, result) => {
            if (err) throw err
            return result
            });
         })
   })
}

I have tried reassigning it to a previously created variable by adding this let res = null, replacing return statement with res = result and returning res at the end of the function and instead.

I have also tried reassigning it by iterating through it by creating an empty array called res and replacing return statement with this :

result.forEach(element => {
    res.push(element)
})

I can console log it by replacing return statement with console.log(result) and it would work normally and print the result set.

I'm starting to figure out that it has something to do with the last line executing before callback finishes executing so it returns nothing. any help is appreciated to guide me to right direction on figuring this out.

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