0
votes

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?

1

1 Answers

0
votes

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
    })


})