gpt4 book ai didi

node.js - 快速验证、自定义异步检查

转载 作者:搜寻专家 更新时间:2023-11-01 00:49:48 25 4
gpt4 key购买 nike

因此,我对此进行了大量研究,但遇到了一些问题。

router.post('/register', async (req, res) => {
const newUser = await usersDb();
// Define the user
const email = req.body.email;
const username = req.body.username;
const password = req.body.password;
const confirmPassword = req.body.confirmPassword;

// Start user already exist check
let userNameCheck = await newUser.findOne({ 'username': username});

req.check('email', 'Email is not valid.').isEmail()
.custom(async value => {
let emailCheck = await newUser.findOne({ 'email': value });
console.log(emailCheck);
console.log('Hmmm')
if (emailCheck !== null) {
return true;
} else {
return false;
}
}).withMessage('Email is already in use.');

//req.check('username', 'Username is required.').notEmpty();
req.check('password', 'Password is required.').notEmpty();
req.check('confirmPassword', 'Confirm password is required.').notEmpty();

// Get errors
let errors = await req.validationErrors();
if (errors) {
console.log(errors);
res.render('index', {
errors: errors
});
} else {
console.log('Still bad');
}
});

我在检查电子邮件时遇到问题。它似乎大部分都在工作,但它没有返回错误。我知道我使用的是同一封电子邮件,而且它正在正确提取它。但是验证不起作用。有什么想法吗?

最佳答案

好吧,这次我成功了,真的:

req.check('email', 'Email is not valid.').isEmail()
.custom(async value => {
let emailCheck = await newUser.findOne({ 'email': value });
if (emailCheck !== null) {
console.log('User Exists');
return Promise.reject();
}
}).withMessage('Email is already in use.');

和:

// Get errors
const errors = await req.getValidationResult();
console.log(errors.mapped())
if (!errors.isEmpty()) {
res.render('index', {
errors: errors.mapped()
});
} else {
console.log('Still bad');
}

关于node.js - 快速验证、自定义异步检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53693650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com