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

1.3K
Views
I don't know how to handle errors in Multer with Express

I am using Multer to upload a maximum of 3 images to the server and it works fine, but when I upload more than 3 images, Multer tells me the error but it is not manageable to show the user.

I'm working on the route that calls the controller but in the Multer documentation it only shows how to handle errors with the route along with the controller actions.

Route:

router.post('/', upload.array('pictures', 3), 
    newProduct
);

Documentation code for handling Multer errors:

const multer = require('multer')
const upload = multer().single('avatar')

app.post('/profile', function (req, res) {
  upload(req, res, function (err) {
    if (err instanceof multer.MulterError) {
      // A Multer error occurred when uploading.
    } else if (err) {
      // An unknown error occurred when uploading.
    }

    // Everything went fine.
  })
})

Error response shown by Multer:

error

In this case how could I do the same documentation but separately to handle Multer errors?

Thank you.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Here multer gives you a middleware to handle the incoming multipart/form-data

const multer = require('multer')
const upload = multer().single('avatar')

You can fix your problem by creating a middleware wrapper to the middleware given by the multer package.

const multer = require('multer');

const uploadMiddleware = (req,res,next)=>{
  
  const upload = multer().array('pictures', 3);

  // Here call the upload middleware of multer
  upload(req, res, function (err) {
     if (err instanceof multer.MulterError) {
       // A Multer error occurred when uploading.
       const err = new Error('Multer error');
       next(err)
       } else if (err) {
       // An unknown error occurred when uploading.
       const err = new Error('Server Error')
       next(err)
     }

    // Everything went fine.
    next()
  })

}

Here with next() you can handle the multer related error your way

And on your routes you can simply use it like this

router.post('/', uploadMiddleware, 
  newProduct
);
over 4 years ago · Santiago Trujillo Report
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!