我正在尝试建立一个使用express-validator的注册页面。但是,无论何时尝试运行我的应用程序,我都会收到以下错误。自从我最近过渡到中间件(6.2)的较新版本以来,我猜测它与Validator部分有关。在此之前,它已经过时了,但是可以正常工作。
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');
}
});
});
});
}
});
第二封电子邮件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)
{
//...
});
也许问题是由这段代码引起的。
const result = validationResult(req);
var errors = result.errors;
根据官方文档result
没有任何errors
属性。您可能需要此var errors = result.array();
,因此错误是任何错误的数组。