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

104
Vistas
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 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