Reputation: 309
I'm using client-sessions (https://github.com/mozilla/node-client-sessions) with async (https://github.com/caolan/async) but am unable to update the user session from within an async waterfall function. I'm wondering if I'm doing something obviously wrong:
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('') })
It seems like the second request should have baz: 'bip'
in the session object, but it doesn't. Why?
Upvotes: 0
Views: 75
Reputation: 309
Face palm! res.send('')
needs to be inside the waterfall complete function or else the response gets sent before the cookie has been updated:
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 }) })
Upvotes: 0