I'm pretty new to Javascript and node.js and I'm having some trouble getting this to work. As far as I know, this should produce the expected result but I'm having some rather weird outputs (cf. comments).
function getHistoryFromDb() {
var rows=1;
pool.getConnection(function (err, connection) {
// rows = 1
console.log(rows);
connection.query('SELECT * FROM messages', function (error, results, fields) {
connection.release();
if (error) throw error;
// rows = 1
rows = results;
// rows = the expected output (array of objects)
});
});
// rows = 1
return rows;
}
Thanks for your help!
EDIT: Looks like it's more an issue linked to the execution being asynchronous EDIT2: Problem solved, had to use callbacks instead of trying to use returns...