Here is the code snippet. In the end, I'm getting an empty array. Please note, I'm new to JavaScript.
Pool is a connection to PostgreSQL database using node-postgres package
function execute_query(pool) {
return new Promise(function(resolve, reject) {
let column_names = ["column1", "column2", "column3"];
let array = [];
for (column_name of column_names) {
query =
"SELECT uniqueid FROM table1 ORDER BY " + column_name + " DESC LIMIT 1";
const sql = {
text: query,
rowMode: "array",
};
pool.query(sql, (err, result) => {
if (err) {
return console.error("Error executing query", err.stack);
} else {
if (result.rows.length > 0) {
array.push(result.rows[0][0]);
}
}
});
}
resolve(array);
});
}
What I actually want is the array with three appended result from the query. As you can see that I'm pushing to array the result.rows[0][0]
I also know that it's an issue of asynchronicity but still, I'm unable to solve it.