【发布时间】:2021-08-20 08:33:21
【问题描述】:
我对 Sequelize 有点陌生,目前遇到了一些问题,我似乎无法找到究竟是什么问题。
当前使用 express-validator 执行自定义验证,如果电子邮件存在,但无论结果如何,它总是会返回一个错误,说电子邮件已经存在于日志中,但在我的邮递员上,它显示无效值,即使它很明显,电子邮件不存在。以下是我的查询
const { check, body, validationResult } = require('express-validator')
const bcrypt = require('bcrypt')
const db = require('../app/models')
const MerchantTemp = db.rest.models.MerchantTemp
const signupValidation = () => {
return [
body('firstname')
.not().isEmpty().trim().withMessage('Firstname field is required'),
body('lastname')
.not().isEmpty().trim().withMessage('Lastname field is required'),
body('phone')
.not().isEmpty().trim().withMessage('Phone Number field is required')
.isNumeric().withMessage('Phone Number field can only contain Numbers')
.isLength({min: 11, max: 13}).withMessage('Phone Number field can only contain minimum of 11 and max of 13 digits respectively'),
body('email')
.not().isEmpty().trim().withMessage('Email Address field is required')
.isEmail().withMessage('Email field is not a valid format').normalizeEmail()
.custom((value, { req }) => {
/**
** HAVING DIFFICULTY HERE
**/
MerchantTemp.findOne({ where: { email: req.body.email } })
.then((result) => {
if (result != null){
throw new Error('Email address is already in use.Please try another one!');
}
}).catch(error => {
throw new Error(error);
})
}),
body('password')
.not().isEmpty().trim().withMessage('Password field is required')
.isStrongPassword(
{
minLength: 6,
minLowercase: 1,
minUppercase: 1,
minSymbols: 1
}).withMessage('Password is too weak. Field must contain min. of 6 characters, 1 lowercase and uppercase character and a symbol')
]
}
const validate = (req, res, next) => {
const errors = validationResult(req)
if (errors.isEmpty()) {
return next()
}
const extractedErrors = []
errors.array().map(err => extractedErrors.push({ msg: err.msg }))
// console.log(errors.array());
res.status(200).json({
statusCode: 400,
errors: extractedErrors
})
}
module.exports = {
signupValidation,
validate
}
对邮递员的回应
{
"statusCode": 400,
"errors": [
{
"msg": "Invalid value"
}
]
}
【问题讨论】:
-
这里的任何帮助将不胜感激
标签: node.js express orm sequelize.js