I'm getting an internal server error while updating the user profile. It's updated when I update with a new avatar but get an error while updating without avatar
Backend Code
exports.updateProfile = catchAsyncErrors(async (req, res, next) => {
const newUserData = {
name: req.body.name,
email: req.body.email,
};
// clodunary
if (req.body.avatar !== "") {
const user = await User.findById(req.user.id);
const imageId = user.avatar.public_id;
await cloudinary.v2.uploader.destroy(imageId);
const myCloud = await cloudinary.v2.uploader.upload(req.body.avatar, {
folder: "avatars",
width: 150,
crop: "scale",
});
newUserData.avatar = {
public_id: myCloud.public_id,
url: myCloud.secure_url,
};
}
const user = await User.findByIdAndUpdate(req.user.id, newUserData, {
new: true,
runValidators: true,
useFindAndModify: false
})
res.status(200).json({
success: true
})
});