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

136
Vistas
Return data from postrgre query in node

How can I proprely return the data I want from a postgre query ?

function getTableName() {
    let queryTableName = `SELECT * FROM admin.information_schema.tables;`;
    let response;

    client.query(queryTableName).then(res => {
        let names = Array();
        for (let i = 0; i < res.rowCount; i++) {
            names.push(res.rows[i]['table_name']);
        }
        response = names;
    }).catch(err => {
        console.error(err);
    });
    return response;
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your code returns the response synchronously, before the query result has arrived from the database asynchronously. Therefore what you return is still undefined.

You must either convert getTableName into an asynchronous function, or implement an express middleware, which is by nature asynchronous:

app.get("/path", function(req, res, next) {
  let queryTableName = `SELECT * FROM admin.information_schema.tables;`;
  client.query(queryTableName).then(resp => {
    let names = Array();
    for (let i = 0; i < resp.rowCount; i++) {
      names.push(resp.rows[i]['table_name']);
    }
    res.json(names);
  }).catch(err => {
    console.error(err);
    next(err);
  });
});
over 4 years ago · Santiago Trujillo Denunciar
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