为什么我要返回 [Object Promise] 而不是回调
Why am I returing an [Object Promise] instead of a callback
我正在尝试设置一个使用 express-validator 的注册页面。但是,每当我尝试 运行 我的应用程序时,我都会收到以下错误。我猜它与 Validator 部分有关,因为我最近过渡到更新版本的中间件 (6.2)。在此之前它已经过时但工作正常。
Error: Route.post() requires a callback function but got a [object Promise]
这里是 POST
// Register - POST
router.post('/register', [
check('userphonenumber', 'a user phone number is required').not().isEmpty(),
check('fname', 'First name field is required').not().isEmpty(),
check('lname', 'Last name field is required').not().isEmpty(),
check('email', 'Email field is required').not().isEmpty(),
check('email', 'Please use a valid email address').not().isEmpty()(),
check('username', 'Username field is required').not().isEmpty(),
check('password', 'Password field is required').not().isEmpty(),
check('passwordConfirm', 'Passwords do not match').equals(body.password),
check('zipCode', 'Zip Code field is required').not().isEmpty(),
], function(req, res) {
// Get Form Values
var fname = req.body.fname;
var lname = req.body.lname;
var email = req.body.email;
var username = req.body.username;
var phonenumber = req.body.phonenumber;
var password = req.body.password;
var passwordConfirm = req.body.passwordConfirm;
var zipCode = req.body.zipCode;
// Check for errors
const result = validationResult(req);
var errors = result.errors;
console.log(errors);
if (!result.isEmpty()) {
res.render('techregister', {
errors: errors,
fname: fname,
lname: lname,
email: email,
username:username,
password: password,
passwordConfirm: passwordConfirm,
zip : zipCode
});
}
else {
var newTech = {
fname: fname,
lname: lname,
phonenumber: phonenumber,
email: email,
username:username,
password: password,
zipCode: zipCode,
}
bcrypt.genSalt(10, function(err, salt){
bcrypt.hash(newTech.password, salt, function(err, hash){
newTech.password = hash;
techdb.users.insert(newTech, function(err, doc){
if(err){
res.send(err);
} else {
console.log('User Added...');
//Success Message
req.flash('success', 'You are registered please confirm with your email before you log in');
// Redirect after register
res.location('/admins/login');
res.redirect('/admins/login');
}
});
});
});
}
});
可能是这段代码导致的问题
const result = validationResult(req);
var errors = result.errors;
根据官方文档 result
没有任何 errors
属性.
你可能想要这个 var errors = result.array();
所以 errors 是一个包含任何错误的数组。
第二封邮件 check
有额外的括号 ()
router.post('/register', [
check('userphonenumber', 'a user phone number is required').not().isEmpty(),
check('fname', 'First name field is required').not().isEmpty(),
check('lname', 'Last name field is required').not().isEmpty(),
check('email', 'Email field is required').not().isEmpty(),
check('email', 'Please use a valid email address').not().isEmpty(),
], function(req, res)
{
//...
});
我正在尝试设置一个使用 express-validator 的注册页面。但是,每当我尝试 运行 我的应用程序时,我都会收到以下错误。我猜它与 Validator 部分有关,因为我最近过渡到更新版本的中间件 (6.2)。在此之前它已经过时但工作正常。
Error: Route.post() requires a callback function but got a [object Promise]
这里是 POST
// Register - POST
router.post('/register', [
check('userphonenumber', 'a user phone number is required').not().isEmpty(),
check('fname', 'First name field is required').not().isEmpty(),
check('lname', 'Last name field is required').not().isEmpty(),
check('email', 'Email field is required').not().isEmpty(),
check('email', 'Please use a valid email address').not().isEmpty()(),
check('username', 'Username field is required').not().isEmpty(),
check('password', 'Password field is required').not().isEmpty(),
check('passwordConfirm', 'Passwords do not match').equals(body.password),
check('zipCode', 'Zip Code field is required').not().isEmpty(),
], function(req, res) {
// Get Form Values
var fname = req.body.fname;
var lname = req.body.lname;
var email = req.body.email;
var username = req.body.username;
var phonenumber = req.body.phonenumber;
var password = req.body.password;
var passwordConfirm = req.body.passwordConfirm;
var zipCode = req.body.zipCode;
// Check for errors
const result = validationResult(req);
var errors = result.errors;
console.log(errors);
if (!result.isEmpty()) {
res.render('techregister', {
errors: errors,
fname: fname,
lname: lname,
email: email,
username:username,
password: password,
passwordConfirm: passwordConfirm,
zip : zipCode
});
}
else {
var newTech = {
fname: fname,
lname: lname,
phonenumber: phonenumber,
email: email,
username:username,
password: password,
zipCode: zipCode,
}
bcrypt.genSalt(10, function(err, salt){
bcrypt.hash(newTech.password, salt, function(err, hash){
newTech.password = hash;
techdb.users.insert(newTech, function(err, doc){
if(err){
res.send(err);
} else {
console.log('User Added...');
//Success Message
req.flash('success', 'You are registered please confirm with your email before you log in');
// Redirect after register
res.location('/admins/login');
res.redirect('/admins/login');
}
});
});
});
}
});
可能是这段代码导致的问题
const result = validationResult(req);
var errors = result.errors;
根据官方文档 result
没有任何 errors
属性.
你可能想要这个 var errors = result.array();
所以 errors 是一个包含任何错误的数组。
第二封邮件 check
有额外的括号 ()
router.post('/register', [
check('userphonenumber', 'a user phone number is required').not().isEmpty(),
check('fname', 'First name field is required').not().isEmpty(),
check('lname', 'Last name field is required').not().isEmpty(),
check('email', 'Email field is required').not().isEmpty(),
check('email', 'Please use a valid email address').not().isEmpty(),
], function(req, res)
{
//...
});