i am a beginner of nodejs and I'm using multer to upload image file. I want the user to be able to submit a form where posting an image is optional However , if i submit and skip the selection of image file , an error will occur: " Cannot read properties of undefined (reading 'destination')". Can anyone tell me how to handle when user submit a form without select image file.
this is code:
const multer = require('multer');
const path = require('path');
const storage = multer.diskStorage({
destination: (req,file,cb) => {
cb(null,'public/images/words');
},
filename:(req,file,cb)=>{
console.log(file);
cb(null, Date.now() + path.extname(file.originalname));
}
});
const upload = multer({storage:storage});
module.exports = upload;