Reputation: 429
I have tried several methods to delete my photo, especially using fs.unlink
but it seems not working at all, you can see from picture below that i save my photos in assets->img->products
and so my database looks like this
and my code looks like this
router.get("/admin/products/:id/delete", (req, res) => {
Product.findByIdAndRemove(req.params.id, (err, photo) => {
if (err) {
req.flash("error", "deleting photo failed");
return res.render("/admin/products/");
}
fs.unlink(photo.image1, function() {
console.log(photo.image1);
return res.redirect("/admin/products");
});
});
});
what is wrong from my code that did not delete my photo from my file?
Upvotes: 0
Views: 91
Reputation: 474
It can not delete photos because you are passing the relative path as the first parameter.
photo.image1 = assets/img/products/image1.jpg
Try passing the passing the absolute path( from the root directory of your machine).
fs.unlink("absolute-path-to-assetsParentFolder" + photo.image1, function() {
console.log(photo.image1);
return res.redirect("/admin/products");
});
Upvotes: 2