gpt4 book ai didi

node.js - Express Validator 的值无效

转载 作者:行者123 更新时间:2023-12-03 22:30:06 24 4
gpt4 key购买 nike

我对 Sequelize 有点陌生,目前遇到了一些问题,我似乎无法找到问题究竟是什么。
当前使用 express-validator 执行自定义验证,如果电子邮件存在,但无论结果如何,它总是会返回一个错误,指出日志中已存在电子邮件,但在我的 postman 身上,即使很明显,它也会显示无效值电子邮件不存在。下面是我的查询

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
}
对 postman 的回应
{
"statusCode": 400,
"errors": [
{
"msg": "Invalid value"
}
]
}

最佳答案

我在这里找到了解决方案 https://express-validator.github.io/docs/custom-error-messages.html
我所做的是替换电子邮件正文的自定义验证中的代码,如下所示

return MerchantTemp.findOne({ where: { email: req.body.email } }).then(user => {
if (user) {
return Promise.reject('E-mail already in use');
}
});

关于node.js - Express Validator 的值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67800783/

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