Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda