this question is sort of an continue from Fetch variable from function into other function (under same function).
Since I created this function, the amount of tables to count is increasing. So I am trying to shorten the code by doing a foreach loop thru an array of table names and then add that count into variable. However the variable that should hold the count stays at 0. When I do a console.log on the returned data from database, then I see the correct database count.
RowDataPacket { c: 8695 } // From database
RowDataPacket { c: 12403 } // From database
0 // Variable that should hold the total count
This is my code so far
socket.on("test", function() {
setInterval(async function() {
dbCon.getConnection(function(err, connection) {
if (err) {
console.log(err);
callback(true);
return;
}
var rows = 0;
var tables = ['logs', 'external_temp'];
tables.forEach(async element => {
await new Promise((resolve, reject) => {
connection.query("SELECT COUNT(id) AS c FROM `" + element + "`", function(error, result) {
if (error) {
reject(error);
}
console.log(result[0]);
rows += result[0].c;
resolve();
});
})
});
var value = (rows).toLocaleString(
undefined, {
minimumFractionDigits: 0
}
);
console.log(value);
connection.release();
});
}, 1000)
});
I am still very much a newbie to javascript and nodejs, so I hope someone can point me into the right direction.