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

126
Vistas
MySQL query not running when called as a method from a different file

When I simply run this code:

var mysql = require('mysql');

class Data {
    static getAllData() {
        console.log("Getting all data...")
        var con = mysql.createConnection({
            host: "localhost",
            user: "myusername123",
            password: "mypassword123",
            database: "mydatabase"
        });

        con.connect(function (err) {
            if (err) throw err;
            var sql = "SELECT * FROM table";
            con.query(sql, function (err, result) {
                if (err) throw err;
                console.log(result);
            });
        });
    }
}


Data.getAllData()

My static method call works, it outputs all the RowDataPacket objects required.

However when I export this class using module.exports.Prompt = Prompt and then call the static method from a different file (I added return result to the method), the code within the con.connect doesn't even seem to run. As in, nothing is returned and nothing is printed out to the console.

Then I just copied the prompt class code into the file I wanted to use it in and then called it in a socket.on('message', data => {}) function and somehow it doesn't seem to work even though it's the same code just in a different file.

Any ideas?

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