In my Mern-Stack I have build a fileuploader using multer and cloudinary. The upload of images works really fine, but when I try to upload a video, the console tells me:
Cannot read property path of undefined
I console.log my Formdata on the whole way. In my dashboard, my redux-slice, my redux-service. Everywhere the value “src” is a file. So it must be multer that not works. Can someone help me out? Thanks
Multer storage:
const multer = require("multer");
const path = require("path");
const maxSize = 1 * 1024 *1024;
const storage = multer.diskStorage({
destination:(req,file, callback)=>{
callback(null, path.resolve(process.cwd(), 'frontside/public/uploads'));
},
fileName: (req, file, callback)=>{
callback(null, Date.now()+ "--"+ path.extname(file.originalname));
console.log(file.originalname)
},
})
const upload = multer({
storage:storage,
fileFilter:(req,file,callback)=>{
if(
file.mimetype == "image/png"||
file.mimetype == "image/jpg" ||
file.mimetype == "image/jpeg" ||
file.mimetype == "video/mp3" ||
file.mimetype == "video/mp4"
){
callback(null, true);
} else{
callback(null,false)
return callback(new Error("Only .png, .jpg, .jpeg, .mp3, .m4 allowed"))
}
},
limits:{fileSize: maxSize}
});
module.exports = upload
backend:
const upload = require("../utils/multer");
const cloudinary = require("../utils/cloudinary");
const path = require("path");
//create
const uploadVideo = upload.single("src");
router.post("/",verifyTokenAndAuthorization, async (req,res)=>{
uploadVideo(req,res, function(err){
if(err){
console.log(err);
return;
} else{
console.log('req.file:', JSON.stringify(req.file));
next();
}
})
try{
const result = cloudinary.uploader.upload_large(req.file.path, {
upload_preset: "Mern_redux-practice",
resource_type: "video",
}
);
const newVideos= new Videos({
cloudinary_id: result.public_id,
ressort: req.body.ressort,
theme: req.body.theme,
title:req.body.title,
src: result.secure_url,
})
const savedVideos= await newVideos.save();
res.status(200).json(savedVideos);
} catch(error){
res.status(403)
console.log(error)
throw new Error("Action failed");
}
});