edit: async (req, res) => {
const { id } = req.params
const updateCampground = await Campground.findByIdAndUpdate(id, { ...req.body.campground })
const imgs = req.files.map(el => ({ url: el.path, filename: el.filename }))
updateCampground.img.push(...imgs)
await updateCampground.save()
if (req.body.deleteImg) {
for (let filename of req.body.deleteImg) {
await cloudinary.uploader.destroy(filename)
}
await updateCampground.updateOne({ $pull: { img: { filename: { $in: req.body.deleteImg } } } })
}
req.flash('success', 'Campground modified successfully')
res.redirect(`/campgrounds/${id}`)
}
The main problem is that line here it dose not work and did not remove the selected images in req.body.deletImg (it's not empty) from img[]
await updateCampground.updateOne({ $pull: { img: { filename: { $in: req.body.deleteImg } } } })
This is img[]
[
{
url: 'https://res.cloudinary.com/dzz5whhe2/image/upload/v1661986678/YelpCamp/jca4is4gpvxrbckmjaxw.jpg',
filename: 'YelpCamp/jca4is4gpvxrbckmjaxw',
_id: new ObjectId("630fe78120e2a31b40febe85")
},
{
url: 'https://res.cloudinary.com/dzz5whhe2/image/upload/v1661986687/YelpCamp/ds9mxvcuafnw3prqg6zp.jpg',
filename: 'YelpCamp/ds9mxvcuafnw3prqg6zp',
_id: new ObjectId("630fe78120e2a31b40febe86")
},
{
url: 'https://res.cloudinary.com/dzz5whhe2/image/upload/v1661995665/YelpCamp/t9qkz1mbg5rastf0ngyn.jpg',
filename: 'YelpCamp/t9qkz1mbg5rastf0ngyn',
_id: new ObjectId("63100a9465bffd5fa369ac28")
},
{
url: 'https://res.cloudinary.com/dzz5whhe2/image/upload/v1661995666/YelpCamp/an95ip1k3etpeikisqzi.jpg',
filename: 'YelpCamp/an95ip1k3etpeikisqzi',
_id: new ObjectId("63100a9465bffd5fa369ac29")
}
]
and this is req.body.deleteImg
[
'YelpCamp/t9qkz1mbg5rastf0ngyn ',
'YelpCamp/an95ip1k3etpeikisqzi '
]
note : the images deleted from cloudinary library but their links still work and they existe somewhere because the URLs in MongoDB is still functional . can someone explain please