PostgreSQL
 sql >> Base de données >  >> RDS >> PostgreSQL

pg client.query() n'attend pas à l'attente

Il semble que vous essayez de faire des rappels et async/attente en même temps.

const {Pool, Client} = require('pg')
const connectionString = 'postgressql://[email protected]:5432/database'

const client = new Client({
    connectionString:connectionString
})

client.connect()

database_func()

async function database_func() {
  // You should be doing callbacks OR async/await whenever you call a query,
  // You're doing both at the same time

  client.query(`SELECT t FROM es ORDER BY t DESC LIMIT 1;`, (err,res) => {
    console.log('res')
    return;
  })

  // OR

  let res;
  try {
    res = await client.query(`SELECT t FROM es ORDER BY t DESC LIMIT 1;`);
  } catch (err) {
    console.error(err);
  }

  console.log(res);
  
  client.end();
  
  console.log('after res')
}