javascript - Nodejs错误消息不断弹出注册系统
问题描述
所以我是 NodeJS 和 JS 的新手,我正在尝试创建一个注册系统。我正在尝试处理我的错误消息。现在,每次在正确填写所有字段后单击注册时,我都会得到以下输出,如图所示。但是,我不确定为什么要在出生日期字段中输入全名以及 2000-11-11,所以我不应该根据我的代码收到这些错误消息。
router.post('/register', function(req, res, next) {
var name = req.body.name;
var email = req.body.email;
var password = req.body.password;
var password2 = req.body.password2;
var bday = req.body.bday;
var birthday = moment(bday);
let errors = [];
// Form Validator
if (!req.body.name) {
errors.push({ msg: 'Please fill in all required fields' });
}
if (req.body.email && !req.body.email.isEmail()) {
errors.push({ msg: 'Please provide an appropriate email' });
}
if (req.body.password2 && req.body.password && req.body.password2 != req.body.password) {
errors.push({ msg: 'Please provide passwords and make sure they match' });
}
if (req.body.bday && !birthday.isValid()) {
errors.push({ msg: 'Date of Birth must be in appropriate format' });
}
if (moment().diff(birthday, 'years') < 13) {
errors.push({ msg: 'User must be at least 13 years of age' });
}
if(errors.length > 0){
res.render('register', {
errors,
name,
email,
password,
password2,
bday
});
} else{
var newUser = new User({
name: name,
email: email,
password: password,
bday: bday,
});
错误信息:
解决方案
推荐阅读
- python - 将图像从一张较大的图像中分离出来
- laravel - 如何在同一个域上指向多个 laravel 项目?
- java - 为什么新用户使用 Spring Boot 注册后无法登录?
- sql - 显示值在组中至少出现一次的所有行
- postgresql - 从 nodejs 服务器发送到 postgres 的查询永远不会超时
- silverstripe - SilverStripe:如何设置/指定 GridField 自定义操作按钮的图标
- constraints - sympy:在范围内对参数化函数进行寻根
- ios - 如何在 Swift 中使用建议栏获取键盘的高度
- python-3.x - pip3 install gdal 错误:命令出错,退出状态为 1
- linux - .Net Core Docker 在 DigitalOcean 上构建时正在删除图像(图片)