当我想在会话中存储数据时,我遇到了一个错误:typeerror:无法设置undefined的属性'user',我知道有类似的问题,但是这些问题都没有解决我的错误。
这是我的密码:
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var express = require('express');
var app = express();
const redis = require('redis');
let redisClient = redis.createClient();
var userSession = session({
secret: 'tada tada',
resave: true,
saveUninitialized: true,
cookie: { secure: false, path: '/', maxAge: 7 * 24 * 60 * 60 * 1000, httpOnly: false},
rolling: true,
store: new RedisStore({
client: redisClient,
host: 'localhost',
port: 6379,
disableTTL: true
})
});
app.use(userSession);
app.use('/',require('./routes/index'));
// app.use('/', userSession, require('./routes/index.js'));
在登录api中,我在会话中存储信息。下面是登录api:
let { username, password } = req.body;
if (username == "..." && password == "...."){
req.session.user = {
"is_logged_in" : true
}
return res.json({
"status" : "success"
});
}
else{
return res.json({
"status" : "fail"
});
}
调用此登录api时,我收到错误消息。
暂无答案!
目前还没有任何答案,快来回答吧!