I am having some trouble figuring out variables "passing" (I know it's not the right term, I'll explain), given the asynchronous nature of node.js.
Please have a look at the following:
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if(err) {
throw err;
}
var solution = rows[0].solution;
});
res.render('index', { title: solution });
As you can imagine, I am getting a reference error, solution is not defined
.
This is because the res.render
is done before getting the solution from the mysql server.
How can I make it render the page once the solution is defined? I know it's something really little and stupid and is really at the very core of node, but please help me understand.