在添加值之前,我必须检查名称是否已存在于数据库中。
所以,我决定添加 express 验证器自定义选项。这在创建调用中工作正常。但不能在更新调用中工作。这是我的代码
const { check, body } = require('express-validator/check'); var models = require("../models");
let Validations = [
check('email').isEmail().withMessage("Invalid Email"),
check('phone').isLength({ min: 5 }).withMessage("Min length Required"),
check('name').not().isEmpty().withMessage("Value is Required"),
body("name").custom(value => {
return models.fundraisers.findByName(value).then(user => {
if (user) {
return Promise.reject('Name already in use');
}
})
})
]
如何在更新调用中处理这个问题。
提前致谢。