我正在使用带有异步(https://github.com/mozilla/node-client-sessions)的客户端会话(https://github.com/caolan/async)但无法在异步瀑布函数中更新用户会话。我想知道我是否做了明显错误的事情:
app.get('/', function (req, res) { console.log(req.session) // first request logs: {}, second request logs: {foo: 'bar'} req.session.foo = 'bar' async.waterfall([ function(callback){ req.session.baz = 'bip' } ], function(){ console.log(req.session) // this logs full object: { foo: 'bar', baz: 'bip' } }) res.send('') })
似乎第二个请求在会话对象中应该有baz: 'bip'
,但它没有。为什么呢?
答案 0 :(得分:0)
面掌! res.send('')
需要在瀑布完成功能内部,否则响应会在更新cookie之前发送:
app.get('/', function (req, res) { console.log(req.session) req.session.foo = 'bar' async.waterfall([ function(callback){ req.session.baz = 'bip' } ], function(){ console.log(req.session) res.send('') // this needed to be moved here }) })