error { "message": "Missing required parameter - file", "name": "Error", "http_code": 400 }
In my frontend is everything okay. Wherever I console.log(img), it is clear that it is a file. Multer in the backend knows that img is a file and send it to the correct folder. So I think, it is cloudinary that not recognizes that img is a file.
That is my backend:
router.post("/", upload.single("img"), verifyTokenAndAuthorization, async (req,res)=>{
const {img, ressort, theme, title, content} = req.body;
try{
const uploadResult = await cloudinary.uploader.upload(img,{
upload_preset: "Mern_redux-practice",
public_id: `${Date.now()}`,
resource_type: "auto",
}).then ((result)=>{
console.log("Upload successfull", JSON.stringify(result, null, 2));
res.status(200).json(result)
}).catch((error)=>{
console.log("error", JSON.stringify(error, null, 2));
});
const newMainNews = new MainNews({
ressort,
theme,
title,
content,
img: {
public_id: uploadResult.public_id,
url: uploadResult.secure_url
}
});
const savedMainNews = await newMainNews.save();
res.status(200).json(savedMainNews);
} catch(error){
res.status(403)
throw new Error("Action failed");
}
});
const MainnewsSchema = new mongoose.Schema({
img:{
public_id: {type:String, required:true},
url:{type:String, required:true},
},
ressort:{type:String, required:true},
theme:{type:String, required:true},
title:{type:String, required:true},
content:{type:String, required:true},
clicked:{type:Number, default:0},
},
{timestamps:true},
);
Thank you, for your answer.
0
-
Hi Roman,
Thank you for reaching out.
I have checked the logs and we are simply not receiving any file so it is not being sent properly to the backend.
Can you do a console.log of the img file before the Cloudinary upload action and see what it says?
Also, please attach your frontend code responsible for making the backend call.
Please let me know if you have any other questions or queries.
Kind Regards,
Thomas0
Post is closed for comments.
Comments
1 comment