使用明上文时,重新定义是不确定的
以下是我的会话设置代码:
import app from './server.js';
import dotenv from 'dotenv';
import mongoose from 'mongoose';
import session from 'express-session';
import {default as connectMongo} from 'connect-mongo';
dotenv.config();
const port = process.env.PORT || 8000;
const MongoURI = process.env.BLOG_DB_URI;
const MAX_AGE = process.env.MAX_AGE;
mongoose
.connect(MongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then((client) => {
const mongoDBstore = connectMongo.create({
mongoUrl: MongoURI,
client: client,
collection: "mySessions"
});
console.log('session: ' + session);
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: mongoDBstore,
cookie: {
path: '/',
httpOnly: false,
maxAge: MAX_AGE,
sameSite: false
}
})
);
app.listen(port, () => console.log(`listening on port: ${port}`));
})
.catch((err) => console.log(err));
以下是我的用户登录代码:
static async loginUser(req, res, next) {
try{
const user = await User.findOne({userName: req.body.userName});
!user && res.status(400).json('Please enter correct username');
const valid = await bcrypt.compare(req.body.password, user.password);
!valid && res.status(400).json('Invalid Password');
const sessUser = {
id: user._id,
userName: user.userName,
email: user.email
};
console.log('req.session: ' + req.session);
req.session.user = sessUser;
res.status(200).json(sessUser);
} catch(err) {
res.status(500).json(`login error: ${err}`);
}
}
现在,当我转到登录路由时:'/api/user/login' 试图登录现有用户,我会遇到错误:
login error: TypeError: Cannot set property 'user' of undefined
在打印重新定义的值时,它显示为未定义。 有人可以让我对我可能做错什么的想法吗? 提前致谢。
Following is my code for session setup:
import app from './server.js';
import dotenv from 'dotenv';
import mongoose from 'mongoose';
import session from 'express-session';
import {default as connectMongo} from 'connect-mongo';
dotenv.config();
const port = process.env.PORT || 8000;
const MongoURI = process.env.BLOG_DB_URI;
const MAX_AGE = process.env.MAX_AGE;
mongoose
.connect(MongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then((client) => {
const mongoDBstore = connectMongo.create({
mongoUrl: MongoURI,
client: client,
collection: "mySessions"
});
console.log('session: ' + session);
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: mongoDBstore,
cookie: {
path: '/',
httpOnly: false,
maxAge: MAX_AGE,
sameSite: false
}
})
);
app.listen(port, () => console.log(`listening on port: ${port}`));
})
.catch((err) => console.log(err));
And below is my code for user login:
static async loginUser(req, res, next) {
try{
const user = await User.findOne({userName: req.body.userName});
!user && res.status(400).json('Please enter correct username');
const valid = await bcrypt.compare(req.body.password, user.password);
!valid && res.status(400).json('Invalid Password');
const sessUser = {
id: user._id,
userName: user.userName,
email: user.email
};
console.log('req.session: ' + req.session);
req.session.user = sessUser;
res.status(200).json(sessUser);
} catch(err) {
res.status(500).json(`login error: ${err}`);
}
}
Now when I'm going over to my login route : '/api/user/login'
and trying to log in an existing user, I'm getting the error:
login error: TypeError: Cannot set property 'user' of undefined
On printing the value of req.session it is showing as undefined.
Can someone please give me some idea on what I might be doing wrong?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题归因于我宣布的中间人的顺序。
app.use(session(...))
必须在app.use('/api/users/login',function(...))
之前。否则res.session
将保持不确定。The issue was due to the order in which I declared the middlewares.
app.use(session(...))
had to come beforeapp.use('/api/users/login', function(...))
. Otherwiseres.session
will remain undefined.