0

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.

HDIK
  • 33
  • 6

1 Answers1

0

I got it working, by using a let global variable, so now it count into the variable, this is my final code.

    socket.on("db_rows", function() {
        setInterval(async function() {
            dbCon.getConnection(function(err, connection) {
                if (err) {
                    console.log(err);
                    callback(true);
                    return;
                }
                
                const 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);
                        }
                        dbRows += result[0].c;
                        resolve();
                        
                    });
                })
                });
                
                connection.release();
                
                var value = (dbRows).toLocaleString(
                    undefined, {
                        minimumFractionDigits: 0
                    }
                );
                
                dbRows = 0;
    
                io.emit("db_rows", value);
            });
        }, timer_rows)
    });
HDIK
  • 33
  • 6