在回调中设置会话值(Node.js,express.js)

时间:2014-04-15 15:40:56

标签: javascript node.js session express

如何在回调中设置会话值?为什么它不起作用?

app.get('/room/:id', function(req, res) {
    var room_id = req.param('id');
    room.getRoom(room_id, function(err, result) {
        if(result.length) {
            req.session.code_room = room_id;
        }
    }); 
    res.render('room.jade');
});

1 个答案:

答案 0 :(得分:1)

你可能应该将res.render移到回调函数的内部:

app.get('/room/:id', function(req, res) {
    var room_id = req.param('id');
    room.getRoom(room_id, function(err, result) {
        if(result.length && !err) {
            req.session.code_room = room_id;
        } else {
            //sorry...
            req.session.code_room = -1;
        }
        res.render('room.jade');
    }); 
});