node.js - 在通过 multer 上传图像之前验证表单数据
问题描述
我正在使用 multer 将图像上传到我的服务器。我使用 express-validator 进行字段验证。我将数据作为多部分/表单数据发送。在图片上传之前,我想验证所有字段验证是否已执行,然后我上传图片,但是当我发送数据并记录它时,正文为空,但图片上传正文后有数据
这行得通
routes.post('/register', upload.single('profile_pic'), validateSignup, signup)
但这未能提供数据
routes.post('/register', validateSignup, upload.single('profile_pic'), signup)
这是我的验证文件
module.exports = {
email: {
in: ['body'],
errorMessage: 'Please enter a valid email address',
exists: true,
isEmail: {
errorMessage: 'Invalid email format'
}
},
password: {
in: ['body'],
isLength: {
errorMessage: 'Password should be at least 6 chars long',
options: { min: 6 }
},
matches: {
options: [/^(?=.*[A-Z])/],
errorMessage: 'At least has a upper letter'
},
exists: true
},
fullName: {
in: ['body'],
errorMessage: 'Please enter a your full name',
isLength: {
options: { min: 3, max: 50 }
},
exists: true
}
}
也可以将 multer 用作函数而不是中间件,因为如果它是一个函数,我可以在任何地方调用它
解决方案
推荐阅读
- python - Python:删除 2 个 csv 文件中的重复元组
- angular - Angular:基于活动路由器链接绑定模板变量的干净方法
- python-3.x - 在 Flask 中执行排序、过滤和分页等操作的简洁方式
- sqlite - 如何在 Pandas 数据框中读取多个 SQLite 表?
- r - 基于 cv.glmnet 中 type.measure 参数的不同结果
- javascript - 使用 axios 检索数据---得到 404 错误
- php - 如何在单击时获取复选框的值
- r - R - IMDb 数据集未加载
- r - 为什么 tidyverse 安装在 Ubuntu 上找不到包?
- php - 如何显示数据库中多行的表格