multer
multer copied to clipboard
Field upload throw error when filter reject the file.
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.
Very interesting! Would you be able to submit a PR with a failing test case?
hi, Having same issue while the uploaded file size is larger than the fileSize value
Also experiencing this issue.
So m i
@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?
@Lone-Wolf17 let me test it again and will share the following test.
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
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