1

I have this piece of code:

/* GET projects page. */
router.get('/projects', function(req, res, next){
    var sess = req.session,
        projects = null;

    if(!sess.login){
        res.redirect('/login?r=projects');
        return false;
    }

    var query = connection.query('SELECT id, name, created, last_edited, edited_by FROM projects', function(err, result) {
        projects = result;
    });

    console.log(projects);

    res.render('projects', {
        title : 'Projects',
        name : sess.login,
        projects : projects
    });
});

I'm trying to get all the results out as an array to be used in the page with jadejs, but projects never gets the data and remains null when called from the router script or the main page.

  • Possible duplicate of [Nodejs mysql connection query return value to function call](http://stackoverflow.com/questions/15635791/nodejs-mysql-connection-query-return-value-to-function-call) – Martin Schneider Jan 18 '16 at 13:55
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ben Fortune Jan 18 '16 at 14:10
  • Try adding if statement in your connection.query statement to check if theres an error, that might give you some more information on whats going on. `if(err){console.log(err)} else{ projects = result;}` – Tomas Jan 18 '16 at 15:15

0 Answers0