Monday, 31 August 2020

Multer and express-validator creating problem in validation

I am submitting a form with an image. Using the below code.

router.post("/", upload.upload('image').single('categoryLogo'), categoryRules.categoryCreationRules(), validate, categoryController.createCategory);

It is working fine, but is some validation comes then still image is saving in disk. so what I tried is :

router.post("/", categoryRules.categoryCreationRules(), validate,upload.upload('image').single('categoryLogo'), categoryController.createCategory);

But in this express validator getting blank body so it throws validation error very time. What should I do for it, I search on google but I did not found any helpful info I am new in the node.

Rules code:

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.")
    ];
}


from Multer and express-validator creating problem in validation

No comments:

Post a Comment