Below is my custom multer function to handle file uploads -
const storage = multer.diskStorage({
destination: (req, file, callback) => {
let type = req.params.type;
let path = `./data/${type}`;
fs.mkdirsSync(path);
callback(null, path);
},
filename: (req, file, cb) => {
const ext = mime.extension(file.mimetype);
const random = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000;
cb(null, `${Date.now()}-${random}.${ext}`);
},
});
Here is how the router is configured -
router.post("/", auth, multer({ storage }).fields([{ name: 'image', maxCount: 1 }, { name: 'video', maxCount: 1 }]), addMovie);
When I log console.log(req.files)
, this is what I see:
[Object: null prototype] {
image: [
{
fieldname: 'image',
originalname: ' .png',
encoding: '7bit',
mimetype: 'image/png',
destination: './data/undefined',
filename: '1631293039713-7613.png',
path: 'data\\undefined\\1631293039713-7613.png',
size: 13133
}
],
video: [
{
fieldname: 'video',
originalname: 'file_example_MP4_480_1_5MG.mp4',
encoding: '7bit',
mimetype: 'video/mp4',
destination: './data/undefined',
filename: '1631293039720-3601.mp4',
path: 'data\\undefined\\1631293039720-3601.mp4',
size: 1570024
}
]
}
I have both an image and a video being uploaded. Now, I want to access the properties within these objects but I'm not sure how.
After trying req.files.image[0]
, here is the error message I received -
Property 'image' does not exist on type '{ [fieldname: string]: File[]; } | File[]'. Property 'image' does not exist on type 'File[]'.ts(2339)