如何设置 req.session 属性

sad*_*laj 2 javascript session mongodb node.js express

我是 Node.js 和会话的新手,我遇到的问题是将属性设置为会话!我尝试将属性添加到会话并将其保存在数据库中,但出现错误

这是我的代码:app.js:(主js文件)

const store = new MongoDBStore({
    uri : 'mongodb://127.0.0.1:27017/OnLineShop',
    collection : 'session'
})

// routes :
app.use(shopRoutes)
app.use('/admin', adminRoutes)
app.use(authroutes)


// session : 
app.use(session(
    {
        secret : 'secret key' , 
        resave : false , 
        saveUninitialized : false , 
        store : store
    }
))
Run Code Online (Sandbox Code Playgroud)

这是我的身份验证处理程序,我将其命名为 auth.js :

 // rout handler for post login requst : 
    module.exports.postLogin = (req , res)=>{
        // session : 
        req.session.isLoggedIn = true 
        res.redirect('/')
    }
Run Code Online (Sandbox Code Playgroud)

在我午餐这个应用程序并想要登录后,出现此错误:

TypeError: Cannot set property 'isLoggedIn' of undefined
    at module.exports.postLogin (/home/sadra/Desktop/projects/web/javascript/shop-app/controllers/auth.js:15:28)
    at Layer.handle [as handle_request] (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/layer.js:95:5)
    at /home/sadra/Desktop/projects/web/javascript/nodejs_frontCast/shop-app/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/index.js:335:12)
    at next (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/index.js:174:3)
    at router (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/sadra/Desktop/projects/web/javascript/nodejs_frontCast/shop-app/node_modules/express/lib/router/index.js:317:13)
    at /home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/index.js:335:12)
    at next (/home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/index.js:275:10)
    at /home/sadra/Desktop/projects/web/javascript/shop-app/node_modules/express/lib/router/index.js:635:15
Run Code Online (Sandbox Code Playgroud)

我不知道我们如何以正确的方式设置会话属性!

Che*_*aik 5

要解决此问题,请更改代码的顺序,

// routes :
app.use(shopRoutes)
app.use('/admin', adminRoutes)
app.use(authroutes)


// session : 
app.use(session(
    {
        secret : 'secret key' , 
        resave : false , 
        saveUninitialized : false , 
        store : strore
    }
))
Run Code Online (Sandbox Code Playgroud)

// session : 
app.use(session(
    {
        secret : 'secret key' , 
        resave : false , 
        saveUninitialized : false , 
        store : strore
    }
))

// routes :
app.use(shopRoutes)
app.use('/admin', adminRoutes)
app.use(authroutes)
Run Code Online (Sandbox Code Playgroud)