Let's say I have an Express app and I want to display something I get from a database. I have a file that contains this (using node-mysql for database interaction)
exports.getData = function() {
connection.query('SELECT * FROM table', function (error, rows, fields) {
return rows[0];
});
}
I'd love to be able to call that and have the data to pass on to the template language. However, it doesn't work that way, because (I think) the call back doesn't return until after the function has exited, resulting in getData returning undefined.
I must be missing some important "aha" moment about callbacks. If I want to interact with the database, am I forced to do all the logic in routes/index.js (the controller for the page I'm serving) and finally render the page at the bottom of a series of callbacks?