0

I have a project, to store every message that pass through mosquitto (mqtt) installed in CentOS7 to mysql database. I use Nodejs to store message that pass through.

And then I display that message in my website. I put "add device" function to add new table in database based on total data every device. (If i had device named "device1" that store temperature and humidity, it will create new table named "device1" and 3 column, id, humidity, and temperature.)

I have a problem. I want to use "results" variable from callback function inside query mysql in javascript. I want to use that variable outside that function.

Here is the code:

var sql= "SELECT count(*) AS hitung FROM information_schema.columns WHERE table_name = 'device' ";

connection.query(sql, function (error, results) {
    if (error) throw error;
    console.log(results[0].hitung);
});

Based on the example I gave above (device1). Console.log will print '3' (because of 3 columns).

I want to use it (the results[0].hitung value) in another function like:

function example() {
    for (i=1; i<=results[0].hitung; i++) {
        console.log(i);
    };
};

But it show error, that I can use that variable. Im new in website development, because i have interest in only networking. Sorry for my bad english, hope you understand my problem. Im new in this community. Thank you.

2 Answers2

1

I want to use it (the results[0].hitung value) in another function like

To do that, you'll need to call example from within your query callback function, exactly like you did with console.log, e.g.:

connection.query(sql, function (error, results) {
    if (error) throw error;
    example(results); // <===
});

That's because:

  1. results is scoped to that callback, and
  2. You have to wait for the callback before you have the results

You might look into using promises, and into using async functions (which create and consume promises). You can use util.promisify to convert Node callback-style functions to promises, although most APIs are actively adopting promises, and there are promise wrapper libs for a lot of other APIS (for instance, mysql-promise for mysql and mysql2). That might help by allowing you to write asynchronous code using standard flow-control mechanisms that used to only work for synchronous code.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Umm, so I cant use it outside that function? sorry for the underscore, i thought it can made my text italic (to highlight that variable) – achmad anshori Sep 12 '19 at 06:57
  • @achmadanshori - No, because it's not available outside the function. Typically one way to handle that is to pass a callback to whatever it is that you have calling `query`.. You may find these questions and their answers useful: [1](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron), [2](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – T.J. Crowder Sep 12 '19 at 06:59
0

You can add a parameter to your function like this:

function example(results) {
    for (i=1; i<=results.length; i++) {
        console.log(results[0].hitung)
    };
};

Then you call the function from your query callback:

connection.query(sql, function (error, results) {
    if (error) throw error;
    example(results);
});
Tom Marienfeld
  • 716
  • 3
  • 14