Express-validator not executing after multer function Express-validator not executing after multer function mongoose mongoose

Express-validator not executing after multer function


Quick Fixes:

  • call singleUpload in middleware, and here you are uploading single file using upload.single(), so you need to remove multiple attribute from view file tag, if you want to upload multiple file then use upload.array('field name', total count of files)
router.post("/list-product",   singleUpload, 
  • validate other parameters in middleware
  • I have noticed you haven't added businessName field in form, so this will return with error, you can add it in form or remove validation from here, and also in schema.
  [    body("productName").trim().isLength({ min: 1 }).withMessage("Please enter the name of your product"),    body("productPrice").isNumeric().withMessage("Please enter a valid price"),    body("productCategory").trim().isLength({ min: 1 }).withMessage("Please select the category of your product"),    body("productDescription").trim().isLength({ min: 50 }).withMessage("Minimum 50 characters").isLength({ max: 500 }).withMessage("Maximum 500 characters"),    body("businessName").trim().isLength({ min: 1 }).withMessage("Please enter the name of your business"),    body("website").trim().isURL().withMessage("Please enter the URL for your product or business"),    check("*").escape()  ], 
  • middleware callback function, here req will provide all body parameters and file object that is uploaded,
  • just console req.body and using req.file will return all details of file that is uploaded in S3, including filename and location, i suggest you to use file name and location from this object, I have console it already,
  • handle file extension error using req.fileTypeInvalid, that we have passed from fileFitler
  function (req, res, next) {    console.log(req.body, req.file);    // FILE EXTENSION ERROR    if (req.fileTypeInvalid) {        res.redirect("list-product" + "?error=" + req.fileTypeInvalid);        return;    }    const errors = validationResult(req);    if (!errors.isEmpty()) {        let errArray = errors.array();        let errorsObj = {}; // access errors indiviually         errArray.map((item) => {            const id = item.param;            delete item.param;            errorsObj[id] = item;        });        res.render("list", {            form: req.body,            errors: errorsObj,            msg: "Please check the form for errors",            option: req.body.productCategory,        });        return;    }    let product = new Product({        business: req.body.businessName,        productName: req.body.productName,        category: req.body.productCategory,        price: req.body.productPrice,        description: req.body.productDescription,        website: req.body.website,        imageURL: "https://mybucket.s3-us-west-2.amazonaws.com/" + req.imageName,    });    product.save(function (err) {        if (err) {            console.log(err);            return next(err);        }        res.redirect("/list-product");    });});

Combined Final Version of Request: