I am trying to update some value in my database using python and mongoose (mongo wrapper library). The value I am trying to update is contained in an array (filename).
Full method
module.exports.updateCampground = async (req, res) => {
const { id } = req.params;
const campground = await Campground.findByIdAndUpdate(id, { ...req.body.campground });
const imgs = req.files.map(f => ({ url: f.path, filename: f.filename }));
campground.images.push(...imgs);
await campground.save();
if (req.body.deleteImages) {
for (let filename of req.body.deleteImages) {
//logs cloudinary conataining uploader, still uploader is undefined
console.log(cloudinary,cloudinary.uploader);
await cloudinary.uploader.destroy(filename);
}
await campground.updateOne({ $pull: { images: { filename: { $in: req.body.deleteImages } } } })
}
req.flash('success', 'Successfully updated campground!');
res.redirect(`/campgrounds/${campground._id}`)
};
This works
const deleteImg = req.body.deleteImages
await campground.updateOne({ $pull: { images: { filename: { $in: deleteImg } } } })
and removes the item however when I do just this it doesn't
await campground.updateOne({ $pull: { images: { filename: { $in: req.body.deleteImages } } } })
Why do I need the const to work? Is it some javascript thing?