Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Multer Prevent uploading if one of the fields is missing

I'm using multer to upload documents.

So i i have 3 fields for users to upload the files .

I'm also using express-validator to validate the fields(wether they are empty or not).

   Upload = multer({

            storage: fileStorage,
            limits: {
              fileSize: 5 * 1024 * 1024
            },
            fileFilter: function (req, file, callback) {
              var ext = path.extname(file.originalname);
              if (ext !== '.png' && ext !== '.jpg' && ext !== '.jpeg' && ext !== ".pdf") {
                return callback(new Error('PDF OR images allowed only'))
              }
      
              callback(null, true)
            },

          }).fields([
            {
              name: 'passport', maxCount: 1
            },
            {
              name: 'Certificate', maxCount: 1
            },
            {
              name: 'personalPhoto', maxCount: 1
            }

          ])

when leaving an empty field the express-validator check() throws an error which is great. but multer keeps uploading the rest of the files.

that's how I'm handling the check() after the upload.

   await check('personalPhoto').custom((value, { req }) => {
            if (!req.files['personalPhoto']) throw new Error("it is required");
            return true;
          }).run(req)

I tried checking if the req.files[file] exist in the fileFilter and throw the error on the callback but the check() keeps throwing error for the 3 files when i check for 1 file only !!

  if(req.files['personalPhoto'] === undefined){

                return callback(new Error('no'))

              }

is there anyway to handle this issue? since you can't use the check() before multer?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!