0

I am pretty new to Javascript, nodejs and socket io, so far everything has worked out, until I needed a new function which pulls data from multiply tables instead of just one table, when it's just one table it works fine, since I can emit from inside the function, but this new function, need to increase a number a few times and then emit it, but for some reason I can't get it to increase from the fetched data inside the query function.

This is my function

socket.on("db_rows", function() {
    setInterval(function() {
        
        var rows = 0;
        
        connection.query("SELECT COUNT(id) AS c FROM `systemusage`", function(error, result) {
            rows += result[0].c;
        });
        
        connection.query("SELECT COUNT(id) AS c FROM `temperature`", function(error, result) {
            rows += result[0].c;
        });
        
        connection.query("SELECT COUNT(id) AS c FROM `bmp280`", function(error, result) {
            rows += result[0].c;
        });
        
        console.log(rows);
        
        //var rows = (dbs.a + dbs.b + dbs.c);
        
        var value = (rows).toLocaleString(
            undefined,
            { minimumFractionDigits: 0 }
        );
            
        io.emit("db_rows", value);
    }, 2000)
});

the rows variable doesn't get increased which means that rows is always 0 and I can't figure out a way to get the fetched data outside of the query function, can someone guide me in the right direction?

edit: I have been googling, but none of the answers has helped out.

1

2 Answers 2

1

connection.query does not wait for a result so all three calls will be executed immediately and on the line console.log(rows); you won't have the correct accumulated value.
You either need to wrap all three calls into promises or use their async counterparts if any.

await new Promise((resolve, reject) => {
  connection.query("SELECT COUNT(id) AS c FROM `systemusage`", function(error, result) {
          if (error) {
             reject(error);   
          }
            rows += result[0].c;
            resolve();
        });  
})

Also you will need to indicate that a callback for setInterval is now an async function:

setInterval(async function() {
...
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much sir, that did the trick. Good to learn new things :-)
0

What about merging the queries in one SELECT? like

connection.query("
    SELECT * FROM (
        SELECT COUNT(id) AS c FROM `systemusage`
        +
        SELECT COUNT(id) AS c FROM `temperature`
        +
        SELECT COUNT(id) AS c FROM `bmp280`
    ) c
    ", function(error, result) {
            rows = result[0];
        });

1 Comment

That statement doesn't work with Mysql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.