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.