Field upload throw error when filter reject the file.
- Dominant language
- JavaScript
- Stars
- 12.1k
- Forks
- 1.1k
- Avg merge
- 8d 2h
- Merged PRs (30d)
- 21
Description
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](https://github.com/AlexAdvent/multer-multiple-issue)
and send put request at url localhost:8000/upload-image after running serve.
Contributor guide
Assessment
This issue has not been assessed yet.