Javascript 检查电子邮件是否在自定义express验证程序的数据库中。(节点、express、mysql)
此代码返回以下错误:“throw err;//Rethrow non-MySQL errors” 我尝试过使用回调和承诺,但我永远不能将错误抛出查询函数之外。我找不到向外部函数发出抛出错误的信号的方法。 我真的很感谢你在这方面的帮助。Javascript 检查电子邮件是否在自定义express验证程序的数据库中。(节点、express、mysql),javascript,node.js,express,express-validator,Javascript,Node.js,Express,Express Validator,此代码返回以下错误:“throw err;//Rethrow non-MySQL errors” 我尝试过使用回调和承诺,但我永远不能将错误抛出查询函数之外。我找不到向外部函数发出抛出错误的信号的方法。 我真的很感谢你在这方面的帮助。 提前谢谢 创建您自己的自定义验证器,并将您的查询封装在承诺中 //Update the user's email endpoint. apiRouter.post('/update-email', [ check('newEmail') .isEmail(
提前谢谢 创建您自己的自定义验证器,并将您的查询封装在承诺中
//Update the user's email endpoint.
apiRouter.post('/update-email', [
check('newEmail')
.isEmail().withMessage('Please Insert a valid Email')
.custom(newEmail=> {
db.query(`SELECT user_id FROM users WHERE email = ?`, newEmail,(err, res)=> {
if(res.length > 0) throw new Error('Email is already registered.');
});
})
], (req, res)=> {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json(errors);
} else {
const newEmail = req.body.newEmail;
const id = req.body.id;
userCRUDfuncs.updateEmail(id, newEmail, db, (err=> {
if(!err) {
return res.status(201).send();
} else {
return res.status(404).send();
}
}));
}
})
创建自己的自定义验证器,并将查询封装在承诺中
//Update the user's email endpoint.
apiRouter.post('/update-email', [
check('newEmail')
.isEmail().withMessage('Please Insert a valid Email')
.custom(newEmail=> {
db.query(`SELECT user_id FROM users WHERE email = ?`, newEmail,(err, res)=> {
if(res.length > 0) throw new Error('Email is already registered.');
});
})
], (req, res)=> {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json(errors);
} else {
const newEmail = req.body.newEmail;
const id = req.body.id;
userCRUDfuncs.updateEmail(id, newEmail, db, (err=> {
if(!err) {
return res.status(201).send();
} else {
return res.status(404).send();
}
}));
}
})
非常感谢。我尝试了async Wait,但给我带来了麻烦。如果它对您有帮助,请将其标记为正确答案:)可能对未来的读者有帮助@伊曼纽尔法斯卡特非常感谢你。我尝试了async Wait,但给我带来了麻烦。如果它对您有帮助,请将其标记为正确答案:)可能对未来的读者有帮助@伊曼纽尔法斯卡