I'm new to Node ... I have a module that connects to a postgres DB through pg-node. In that I have no problems. My problem is that when calling that module, from another module, and then wanting to use the data that came out of the BD, it says undefined ... I'm almost sure it's because the connection to the DB returns a promise, and the data does not They are available at the time of use. Pego code
PG connection module
module.exports = function (query) {
const { Client } = require ('pg')
const connection = {
user: 'postgres',
host: 'localhost',
database : 'hoteltest',
password: '1234',
port: 5432
}
const client = new Client(connection)
var datos;
client.connect()
client.query(query)
.then (response => {
datos = response.rows
console.log(datos)
return datos
client.end()
})
.catch(err => {
console.log(err)
client.end()
})
}
and so I call this module:
import data from './pg/index.js'
var pgdata;
pgdata = data('SELECT * FROM usuarios')
console.log(pgdata)
Any suggestions? Thanks!