So I have a POST function that uses the database.each() method in it. I'm also sending an array (via response.send()) that is updated by the .each() method at the end of said function. The only problem is that the array is sending before the database.each() method runs properly. I tried using async/await but for some reason it wasn't working...here's the function:
app.post('/', async function(request, response) {
var array = []
console.log("Got a POST request");
let data = JSON.stringify(request.body);
await db.each(sqlcommand, [variable], (err, row) => {
if (err) {
throw err;
}
let jsonObject = {
variable1 : row.var1,
variable2 : row.var2,
variable3 : row.var3
}
array(JSON.stringify(jsonObject))
});
response.send(array)
});