[英]NodeJS:-How to store image from a form in mongo DB using multer and send the same image to mail as an attachment
[英]Mongo/Mongoose/Multer/FS to delete an image
在下面给出的代码中:
var Image = mongoose.model("Image", imageSchema); //Assuming all the configuration of packages are done app.delete("/element/:id", function(req, res) { Image.findByIdAndRemove(req.params.id, function(err) { if(err) { //Error Handling } else { fs.remove(path, function(err) { //where path is the path of the image and fs is fs-extra if(err) { //Error Handling } else { res.send("Image deleted!"); } }); } }); });
假设猫鼬成功从数据库中删除了数据。 但是fs(即fs-extra)无法删除该图像。 然后将出现一种方案,其中删除了数据但图像仍然存在。 那么有没有办法处理这种情况呢?
使用fs.unlink
代替fs.remove
。
var Image = mongoose.model("Image", imageSchema); //Assuming all the configuration of packages are done
app.delete("/element/:id", function(req, res) {
Image.findByIdAndRemove(req.params.id, function(err) {
if(err) {
//Error Handling
} else {
fs.unlink(path+req.file.filename, (err) => {
if (err) {
console.log("failed to delete local image:"+err);
} else {
console.log('successfully deleted local image');
}
});
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.