multer icon indicating copy to clipboard operation
multer copied to clipboard

Field upload throw error when filter reject the file.

Open AlexAdvent opened this issue 2 years ago • 8 comments

I am uploading two image using .fields([{ name: 'certificateImage', maxCount: 1 }, { name: 'addressProofImage', maxCount: 1 }]); but it is throwing error when filefilter reject the file

error

  if (this.req.files[placeholder.fieldname].length === 1) {
                                                ^

TypeError: Cannot read properties of undefined (reading 'length')

Multer throw error at line 49 in file-appender.js. req.files[placeholder.fieldname] is showing undefined.

case 'OBJECT':
      if (this.req.files[placeholder.fieldname].length === 1) {
        delete this.req.files[placeholder.fieldname]
      } else {
        arrayRemove(this.req.files[placeholder.fieldname], placeholder)
      }
      break

My code

const multer  = require('multer');
const path = require('path');

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      console.log('type',req.body.type);
      cb(null, "uploads/images/");
    },
    filename: function (req, file, cb) {
      cb(
        null,
        file.fieldname + "-" + Date.now() + path.extname(file.originalname)
      );
    },
  });


const maxSize = 1 * 1024 * 1024; // for 1MB

var upload = multer({
    storage: storage,
    fileFilter: async (req, file, cb) => {

      var ext = path.extname(file.originalname);
      console.log("ext", ext);

      let validImageExtensions = ['png', 'jpg', 'jpeg'];

      if (validImageExtensions.indexOf(ext.substring(1)) === -1) {
        cb(null, false);
        console.log("invalid extension");
        return cb(new Error("Only " + validImageExtensions + " are allowed with maxsize 1MB"));
      }else{
        console.log("valid extension");
        cb(null, true);
      }
    },
    limits: { fileSize: maxSize },
  });

module.exports = upload;

everything work fine if file extension match and image store in upload folder but throw same error when extension doesnt not match.

I have created github repo for the you to better to test error. https://github.com/AlexAdvent/multer-multiple-issue

and send put request at url localhost:8000/upload-image after running serve.

AlexAdvent avatar May 20 '22 06:05 AlexAdvent

Very interesting! Would you be able to submit a PR with a failing test case?

LinusU avatar May 29 '22 10:05 LinusU

hi, Having same issue while the uploaded file size is larger than the fileSize value

geminigeek avatar Jul 08 '22 11:07 geminigeek

Also experiencing this issue.

a-k-o-r-e-d-e avatar Aug 08 '22 14:08 a-k-o-r-e-d-e

So m i

andreicimpoesu avatar Aug 22 '22 17:08 andreicimpoesu

@LinusU I am willing to submit a PR with a failing test case. but to be honest, I have no idea how to generate such a test case. can you please point out a PR where someone submitted something similar and will work it out from there?

a-k-o-r-e-d-e avatar Aug 23 '22 15:08 a-k-o-r-e-d-e

@Lone-Wolf17 let me test it again and will share the following test.

AlexAdvent avatar Aug 23 '22 15:08 AlexAdvent

I have created github repo to test the error for the you to better to test error. https://github.com/AlexAdvent/multer-multiple-issue

@LinusU , @geminigeek , @andreicimpoesu , @Lone-Wolf17

AlexAdvent avatar Aug 24 '22 19:08 AlexAdvent

you should remove this line:

cb(null, false)

and you should just return this line:

cb(new Error("..."))

if you send both of them you will give an error before second call because the callback function that return "false" instead of a error instance try to check additional conditions that we don't need to check them

so you just need to send second callback with a instance of Error

Miladxsar23 avatar Feb 18 '23 07:02 Miladxsar23