Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!