I'm a C/C++ guy trying to make a simple web app using Node.js, Express.js and MariaDB.
function getData(sensor)
{
var query = c.query('SELECT * FROM sensor_data WHERE id >= ALL (SELECT id FROM sensor_data WHERE sensor = ?) AND sensor = ?', [sensor,sensor]);
query.on('result', function(res) {
res.on('data',function(row) {
return(row);
});
});
}
app.get('/', function(req, res) {
res.render('index', {
temperatureSala: getData('sala').temperature + 'ºC',
humiditySala: getData('sala').humidity + '%'
});
});
My problem is dealing with the asynchrony. When I try to render the page the render functions finishes running first and then I get that the variables are undefined. The query finishes after and the result is correct.
What is the best way of dealing with this? Any question you have please ask.
Thank you!