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

102
Visualizações
waiting for database to connect before exporting module functions to controller

I am getting the error below:

Service.getUsers is not a function

I recently changed my code to include this functionality:

async function connectToDatabase(secret) {
  try {
      const secretValue = await getSecretPromise(secret)

      const { host, port, username, password, dbname } = JSON.parse(secretValue);

      con = mysql.createConnection({
          host: host,
          port: port,
          user: username,
          password: password,
          database: dbname
      });
      con.connect(function(err) {
        if (err) throw err;
        console.log("Connected to database!");
      });
  } catch (error) {
      console.error(error)
  }
}

connectToDatabase(secretName).then(function() {
  module.exports = {
    createUser,
    getUsers,
    patchUser,
    loginUser,
    updateCallForward,
    getEmail,
    getCallForwardingNumber,
  };
});;

Basically, it will only export the functions to the controller if the database is connected and the value of con is initialized (this ensures that the value of con is not undefined which has been troubling me). However, when I do this, it messes with the controller js and I get the error above.

var accountService = require('./account.service');
router.get('/get-users', getUsers);

async function getUsers(req, res, next) {

    accountService.getUsers().then(function(val) {
        res.json(val)
    });
}

I believe this is because of the waiting that is employed in the service.js. Is there anyway I can make the controller wait for the functions to get initialized through the export?

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