I'm trying to query data from my postgresql database. I'm using pg and I query the data using client.query. I'm successfully making the query, but I lose the data when I leave the function called by client.query. I'm not sure if it has anything to do with the await, but I need that to get it working.
var data;
const connectionString = 'postgres://postgres:postgres@storedb.csyjfyng5vgc.us-west-2.rds.amazonaws.com:5432/clientdb';
const client = new Client({
connectionString: connectionString
});
await client.connect();
await client.query('SELECT * from public.clients', function (err, res) {
if (err) {
console.log("This is the Error")
console.log(err);
return err;
}
client.end();
data = res.rows;
});
console.log("data", data);
res.rows gives me the array that I want, but when I set it to data, data is undefined. Console.log("data", data) returns:
data undefined
How can I make this data persist outside of the function called by client.query?