如何在回调中设置会话值?为什么它不起作用?
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');
});
答案 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');
});
});