【发布时间】:2020-12-17 07:17:03
【问题描述】:
我正在提交带有图片的表单。使用下面的代码。
router.post("/", upload.upload('image').single('categoryLogo'), categoryRules.categoryCreationRules(), validate, categoryController.createCategory);
它工作正常,但是经过一些验证后,静止图像会保存在磁盘中。 所以我尝试的是:
router.post("/", categoryRules.categoryCreationRules(), validate,upload.upload('image').single('categoryLogo'), categoryController.createCategory);
但是在这个快速验证器中得到空白的主体,所以它会抛出验证错误。 我该怎么办,我在谷歌上搜索,但我没有找到任何有用的信息我是节点中的新手。
规则代码:
const categoryCreationRules = () => {
return [
check('name')
.isLength({ min: 1 })
.trim()
.withMessage("Category name is required."),
check('name').custom((name)=>{
return CategoryModel.findOne({name: name}).collation({locale:'en',strength: 2})
.then(category=>{
if(category){
return Promise.reject(category.name+" category already exsist.");
}
})
}),
check('name')
.isLength({max: 100})
.trim()
.withMessage("Category name should not exceed more then 100 characters."),
check('description')
.isLength({max: 255})
.trim()
.withMessage("Category description should not exceed more then 255 characters.")
];
}
【问题讨论】:
-
可以添加
categoryCreationRules的代码吗? -
代码已添加.in
标签: node.js express multer express-validator