首页 > 解决方案 > 快速路由参数检查+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>
.
.

标签: node.jsexpress

解决方案


推荐阅读