This may be a stupid question but I was not able to find an answer. My db query (node -> PostgreSQL) is not firing the callback when the query function itself is called from another callback like this:
routes.js
router.post("/getsheet/", (req, res) => {
googleAPI.getSheet(googleToken, req.body.sheetid).then((invoices) => {
templateData.sheetData = invoices;
templateData.sheetData.length = invoices.length;
// When sheet is received, db query runs but it's callback won't
// function takes an array and index
db.query(invoices, 1).then((db_results) => {
console.log(db_results);
res.redirect("/");
}).catch((db_error) => {
console.error(db_error)
res.redirect("/");
});
}).catch((error) => {
console.error(error);
});
});
db.js
const query = (data, index) => {
return new Promise((resolve, reject) => {
// console log fires but not the callback function so it does not resolve or reject
console.log("... querying index ... " + index + " customer: " + data[index].name);
client.query(`SELECT * FROM customer WHERE customer_number=${data[index].customer_id};`, (err, res) => {
// does not run this block
if (!err) {
resolve(res);
} else {
return reject(err);
}
});
});
}
Thanks! :)
routes.jsdoesn't even call thequeryfunction you show after. Something is missing. And in general, instead of convertingpglibrary into promises, it is better to use pg-promise ;)