控制器没有正常工作
controller isnt working as it is supposed to
我使用 MVC 制作了一个 NodeJS 服务器,这是控制器之一:
module.exports.create_user = async function (req, res) {
// console.log(req.body);
// console.log(req.user);
await Company.findOne({ user: req.body.user }, function (err, user) {
if (user) {
return res.redirect('/login');
}
else {
if (req.body.password == req.body.confirm_password) {
Company.create({
"country": req.body.country,
"username": req.body.user,
"password": req.body.password
});
}
else {
console.log('Passwords didnt match');
}
}
});
req.session.save(() => {
return res.redirect('/profile');
})
}
这段代码应该做什么?
搜索用户是否已经存在;如果是,它将重定向到 /login
。
如果不存在这样的用户,它应该创建一个新用户并重定向到 /profile
.
这段代码有什么作用?
无论用户是否存在,代码总是重定向到/login。另外,在数据库中创建了一个用户,所以每次有新用户要注册时,用户都需要先注册,然后去登录才能获得/profile
的访问权限
不允许重定向到 /profile
的问题是什么?以及如何解决?
如果您还需要什么,请告诉我
使用 username
而不是 user
来查找用户
Company.findOne({ username: req.body.user });
您正在混合使用callback
样式和async/await
,await
关键字对您没有影响,它不会等到查询完成。 await
关键字仅在您等待 Promise like object
(then
可用对象)时起作用。
我猜你用的是mongoose
,当前版本mongoose
支持Promisereturn风格
module.exports.create_user = async function (req, res) {
// console.log(req.body);
// console.log(req.user);
try {
// Use `username` instead of `user` to find a user
const user = await Company.findOne({ username: req.body.user }); // callback is not passed, it will return a Promise
if (user) {
return res.redirect('/login');
}
if (req.body.password == req.body.confirm_password) {
await Company.create({ // wait until user is created
"country": req.body.country,
"username": req.body.user,
"password": req.body.password
});
// then redirect page
req.session.save(() => {
return res.redirect('/profile');
});
} else {
console.log('Passwords didnt match');
// what happen when password didn't match
// return res.redirect('/login'); ???
}
} catch (error) {
// something went wrong
// return res.redirect('/login'); ???
}
}
passport.checkAuthentication = async function (req, res, next) {
console.log(req.user);
let auth_status = await req.isAuthenticated() ? "sucess" : "failure";
console.log(`Authentication ${auth_status}`);
// if the user is signed in, then pass on the request to the next function(controller's action)
if (await req.isAuthenticated()) {
return next();
}
// if the user is not signed in
return res.redirect('/login');
}
我在这方面做了更多的工作,控制器可能工作正常,问题可能出在中间件上。在上面讨论的注册案例中,中间件总是将 'Authentication failure' 记录到控制台。
我使用 MVC 制作了一个 NodeJS 服务器,这是控制器之一:
module.exports.create_user = async function (req, res) {
// console.log(req.body);
// console.log(req.user);
await Company.findOne({ user: req.body.user }, function (err, user) {
if (user) {
return res.redirect('/login');
}
else {
if (req.body.password == req.body.confirm_password) {
Company.create({
"country": req.body.country,
"username": req.body.user,
"password": req.body.password
});
}
else {
console.log('Passwords didnt match');
}
}
});
req.session.save(() => {
return res.redirect('/profile');
})
}
这段代码应该做什么?
搜索用户是否已经存在;如果是,它将重定向到 /login
。
如果不存在这样的用户,它应该创建一个新用户并重定向到 /profile
.
这段代码有什么作用?
无论用户是否存在,代码总是重定向到/login。另外,在数据库中创建了一个用户,所以每次有新用户要注册时,用户都需要先注册,然后去登录才能获得/profile
不允许重定向到 /profile
的问题是什么?以及如何解决?
如果您还需要什么,请告诉我
使用 username
而不是 user
来查找用户
Company.findOne({ username: req.body.user });
您正在混合使用callback
样式和async/await
,await
关键字对您没有影响,它不会等到查询完成。 await
关键字仅在您等待 Promise like object
(then
可用对象)时起作用。
我猜你用的是mongoose
,当前版本mongoose
支持Promisereturn风格
module.exports.create_user = async function (req, res) {
// console.log(req.body);
// console.log(req.user);
try {
// Use `username` instead of `user` to find a user
const user = await Company.findOne({ username: req.body.user }); // callback is not passed, it will return a Promise
if (user) {
return res.redirect('/login');
}
if (req.body.password == req.body.confirm_password) {
await Company.create({ // wait until user is created
"country": req.body.country,
"username": req.body.user,
"password": req.body.password
});
// then redirect page
req.session.save(() => {
return res.redirect('/profile');
});
} else {
console.log('Passwords didnt match');
// what happen when password didn't match
// return res.redirect('/login'); ???
}
} catch (error) {
// something went wrong
// return res.redirect('/login'); ???
}
}
passport.checkAuthentication = async function (req, res, next) {
console.log(req.user);
let auth_status = await req.isAuthenticated() ? "sucess" : "failure";
console.log(`Authentication ${auth_status}`);
// if the user is signed in, then pass on the request to the next function(controller's action)
if (await req.isAuthenticated()) {
return next();
}
// if the user is not signed in
return res.redirect('/login');
}
我在这方面做了更多的工作,控制器可能工作正常,问题可能出在中间件上。在上面讨论的注册案例中,中间件总是将 'Authentication failure' 记录到控制台。