Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images are not deleted from cloudinary when the whole campground is deleted. #40

Open
TalibIbrahim opened this issue Apr 1, 2023 · 2 comments

Comments

@TalibIbrahim
Copy link

The images are only deleted when we edit the campground. Deleting campground deletes the link to it, not the image.

@TalibIbrahim
Copy link
Author

module.exports.deleteCampground = async (req, res) => {
  const { id } = req.params;

  const campground = await Campground.findById(id);
  const images = campground.images;
  const filenames = images.map((image) => image.filename);
  console.log(filenames); // prints an array of all the image filenames
  for (let filename of filenames) {
    cloudinary.uploader.destroy(filename);
  }

  await Campground.findByIdAndDelete(id);

  req.flash('success', 'Successfully deleted Campground.');
  res.redirect('/campgrounds');
};

This code should work for deleting images from cloudinary.

@Alpha-Stark
Copy link

The images are only deleted when we edit the campground. Deleting campground deletes the link to it, not the image.

module.exports.deleteCampground = async (req, res) => {
const { id } = req.params;
const campground = await Campground.findById(id);
for (const img of campground.images) {
cloudinary.uploader.destroy(img.filename);
}
await Campground.findByIdAndDelete(id);
req.flash("success", "Successfully Deleted a Campgound.!");
res.redirect("/campgrounds");
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants