I am making a project of tweet backend . I want to use image for user profile and cover image and i implemented validation using express validator. my aproach is first upload file on server and then on cloudinary and then unlink from server. this is working fine in all conditions .
But when the validation failed the file uploaded on server but not unlink from there because** I use validation logic after multer middleware.
**
so the file upload on server before validation.
router.route("/register").post(
upload.fields([
{name:"avtar",maxCount:1},
{name:"coverImage",maxCount:1}
]),
userRestrationValidation(),
validate,
registerUser)
To overcome this problem i add validation before multer midleware (between the express validation and controller). because the validation performs before file upload and in the fail condition the file does not upload on server
router.route("/register").post(
userRestrationValidation(),
validate,
upload.fields([
{name:"avtar",maxCount:1},
{name:"coverImage",maxCount:1}
]),
registerUser)
but in this aproach the express validation fails and throw error in all correct and valid field.
so, please solve this problem.
thanks in advance
I tried in multiple ways but the problem is same.
can we not use the muddleware in between the express validator and the controller