node.js - 快速路由参数检查+Multer
问题描述
我正在尝试将图像上传到数据库并同时检查其他值。但是,即使标题长度超过 3,它也总是返回错误...如果我删除了所有检查或删除了 upload.single('image'),则该代码有效。那么有没有办法让他们两个在同一个帖子中?
管理员路由.js
.
.
router.post('/addProduct', [
check('title').isLength({min: 3}).withMessage('Title should be more than five'),
check('description').isLength({min: 5}).withMessage('description should be more than five'),
check('available_stock').isLength({min: 1}).withMessage('location should be more than five'),
check('price').isLength({min: 1}).withMessage('The price is'),
check('manufacturer').isLength({min: 3}).withMessage('manufacturer should be valid'),
],upload.single('image'), (req,res)=> {
const errors = validationResult(req)
if (!errors.isEmpty()) {
req.flash('errors', errors.array())
res.redirect('/shopAdmin/addProduct')
}
else {
let Pro= new Product({
title: req.body.ProName,
description: req.body.ProDes,
available_stock: req.body.ProAvStk,
price: req.body.ProPrice,
manufacturer : req.body.ProManu,
proImg: req.file.filename,
})
Pro.save((err)=>{
if(!err){
console.log("ADDED ")
.
.
addProduct.ejs
<form action="/shopAdmin/addProduct" method="post" enctype="multipart/form-data">
.
<div class="form-group">
<label for="image">UPLOAD</label>
<input type="file" name="image" id="image" class="form-control">
</div>
<div class="mt-2 form-group" >
<button type="submit" class="btn btn-primary">Create</button>
</div>
</form>
.
.
解决方案
推荐阅读
- c# - 禁用/启用 USB 端口 Android Xamarin
- laravel - Laravel Firebase 数据消息在颤振 onBackgroundMessage 中不起作用
- excel - 使用条件更改按钮中的文本
- loops - 将每个 for 循环迭代的输出保存到文件中 - python
- excel-addins - 脚本实验室自定义函数功能不起作用
- javascript - 我们可以替换 Math.floor 并且永远不必再次使用它吗?
- android - VOIP 应用程序 - 如何在正在进行的 VOIP 通话期间使外部应用程序的通知声音和振动静音?
- c++ - 在链表尾部插入元素的插入函数
- python-3.x - 重新加载服务器时 Django 损坏
- css - 如何创建带有背景图像的css文本框,文本不应溢出,并且必须包裹在图像内