multer icon indicating copy to clipboard operation
multer copied to clipboard

multer.array removes null items

Open dimzeta opened this issue 2 years ago • 0 comments

When my endpoint receives an array of pictures, some elements can be null. My issue is multer ignores null values, and it's impossible to me to associate the index with the file.

Is there a way to do that? If needed I can update my frontend code.

Front:

const formData = new FormData();
this.form.pictures.forEach(player => {
  formData.append('photos', player.picture);
})
 await axios.post("/photos/upload", formData);

Payload example:

photos: false
photos: (binary)
photos: null

Back:

router.post(
  "/photos",
  multer({dest: uploadsPath + "/photos"}).array('photos', 6),
  async (req, res) => {
    console.log(req.files);
  }

Result log:

[
  {
    fieldname: 'photos',
    originalname: 'very-small.jpg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    destination: 'backend/public/uploads/photos',
    filename: 'ddf16ac69d5288370b599401a87363fd',
    path: 'backend/public/uploads/photos/ddf16ac69d5288370b599401a87363fd',
    size: 20827
  }
]

dimzeta avatar Jan 31 '22 17:01 dimzeta