[英]i am not getting req.session in error handling middleware my Node and express app?
这是我得到的错误和未定义,当我在我的错误处理中间件中使用 console.log (req.session) 并访问 req.session.isLoggedIn 时
cookie: {
path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true,
secure: null,
domain: null,
sameSite: null
},
csrfSecret: 'TAmB7luZ3vUTFd0FWmsWoXHP',
flash: {},
isLoggedIn: true,
user: {
cart: { items: [] },
_id: 5f1adb6163d0bc36506a6696,
email: 'bella@gmail.com',
password: '$2a$12$HCaoOhpMJiBEIw177tR4LuMV62a3zlnWFz4RQZYH4TPzit/vYRfn2',
__v: 1
}
}
undefined
TypeError: Cannot read property 'isLoggedIn' of undefined
at D:\Node-Project\app.js:110:34
at Layer.handle_error (D:\Node-Project\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (D:\Node-Project\node_modules\express\lib\router\index.js:315:13)
at D:\Node-Project\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\Node-Project\node_modules\express\lib\router\index.js:335:12)
at next (D:\Node-Project\node_modules\express\lib\router\index.js:275:10)
at Layer.handle_error (D:\Node-Project\node_modules\express\lib\router\layer.js:67:12)
at trim_prefix (D:\Node-Project\node_modules\express\lib\router\index.js:315:13)
at D:\Node-Project\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\Node-Project\node_modules\express\lib\router\index.js:335:12)
at next (D:\Node-Project\node_modules\express\lib\router\index.js:275:10)
at Layer.handle_error (D:\Node-Project\node_modules\express\lib\router\layer.js:67:12)
at trim_prefix (D:\Node-Project\node_modules\express\lib\router\index.js:315:13)
at D:\Node-Project\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\Node-Project\node_modules\express\lib\router\index.js:335:12)
at next (D:\Node-Project\node_modules\express\lib\router\index.js:275:10)
In case of a POST request, req.session.isLoggedIn
works only when the session is saved previously.在 POST 请求的情况下, req.session.isLoggedIn
仅在先前保存会话req.session.isLoggedIn
起作用。 Here, you have started the session and then completed it before assigning the value to isAuthenticated
.在这里,您已经启动了会话,然后在将值分配给isAuthenticated
之前完成了它。 Since, there is no session running during the assignment operation, it shows 'undefined'.由于在分配操作期间没有运行会话,它显示“未定义”。 You have the value of req.session.isLoggedIn
in res.locals.isAuthenticated
.您在req.session.isLoggedIn
中有res.locals.isAuthenticated
的值。 So, replace req.session.isLoggedIn
with res.locals.isAuthenticated
and it will solve your problem as of now.因此,将req.session.isLoggedIn
替换为res.locals.isAuthenticated
它将解决您现在的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.