success in deleting but...
I recently made an API with Cloudinary and NodeJS, of course, this API has the function to delete images, but when I went to test it, there was always a problem. The API returns 200, it seems like everything is perfect, but if I go to Cloudinary, it didn't delete anything. In fact, the image is still there. Could someone help me and tell me if there is something wrong? I did it following the docs.
/* delete photo by appointment id and photo id */
router.delete('/:appointmentId/photos/:photoId', async (req, res) => {
const { appointmentId, photoId } = req.params;
try {
const appointment = await Appointment.findById(appointmentId);
if (!appointment) {
return res.status(404).json({ message: 'Appointment not found' });
}
const photoToDelete = appointment.photos.find(
photo => photo.public_id.toString() === photoId
);
if (!photoToDelete) {
return res.status(404).json({ message: 'Photo not found' });
}
try {
await cloudinary.uploader.destroy(photoToDelete.public_id);
} catch (cloudinaryError) {
console.error('Error deleting photo from Cloudinary:', cloudinaryError);
return res.status(500).json({ error: 'Error deleting photo from Cloudinary' });
}
appointment.photos = appointment.photos.filter(
photo => photo._id.toString() !== photoId
);
await appointment.save();
res.status(200).json({
message: 'Photo deleted successfully',
data: appointment,
});
} catch (error) {
console.error('Error deleting photo:', error);
res.status(500).json({ error: 'Error deleting photo' });
}
});