The goal is to have theQuery function handle the query and return an array of the rows back to result. In the theQuery function console.log works as expected, but logging result logs undefined. The undefined log occurs first on the the terminal. How can a promise be used to fix this?
"use strict";
var pg = require('pg').native;
var connectionString = "...";
var client = new pg.Client(connectionString);
client.connect();
function theQuery(table,column) {
client.query('SELECT * FROM "'+table+'" WHERE column = '+"'"+column+"';", function(err, result) {
if(err) {
return console.error('error running query', err);
}
console.log(result.rows);//works logs the rows
return result.rows;
});
}
let Query = theQuery("table", "column);
console.log(result);// logs undefined