Search code examples
node.jskoa

Cannot store data in koa.js app using koa-session


I'm using koa-session in my koa-app to store some data. So here is my code:

/*some code*/
app.use(session(app));
/*some code*/
router.get('/test', async (ctx) => {
    const {session} = ctx;

    console.log(session['test']) // always undefined

    session['test'] = 'test'
});

I'm expect to get undefined on the first request and 'test' on the other requests, but I'm always getting undefined. What is wrong with this code?


Solution

  • try

    ctx.session.test = "testsessiondata"