client-sessions nodejs / Express – 错误:无法设置未定义的属性“mydata”

我试图在nodejs / Express中使用客户端会话中间件,并且出现以下错误: Cannot set property 'mydata' of undefined.

我也看了这个post,但是找不到更多的线索,为什么我可能会得到这个错误。 节点-JS-客户端会话而不是创造-REQ会话

 var bodyParser = require('body-parser'); const express = require('express'); const app = express(); const clientSessions = require("client-sessions"); var urlencodedParser = bodyParser.urlencoded({ extended: false }); var router = express.Router(); app.use(clientSessions({ cookieName: 'mydata', // cookie name dictates the key name added to the request object secret: 'longsecretkeyorwhatever', // should be a large unguessable string duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms activeDuration: 1000 * 60 * 5 // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds })); /* Form POST handler */ router.post('/', urlencodedParser, function(req, res) { if (!req.body) return res.sendStatus(400); if(req.body.firstName){ req.session_state.mydata = req.body.mydata; } }) 

client-sessions的文档解释:

cookie名称指定添加到请求对象的密钥名称

由于您将firstName设置为cookie名称,因此会话对象可用作req.firstName

 req.firstName.mydata = req.body.mydata