//You must require fs and path before this action can be done //set this on the top of the model const imagePath = 'uploads/image' //Create a virtual with the schema name e.g. blogSchema.virual('imagePath').get(function(){ if(this.image != null){ return path.join('/', imagePath, this.image) } }) //exports the imagePath as like this module.exports.imagePath = imagePath //go to the route and require it with the model name e.g. const uploadPath = path.join("public", BlogModel.imagePath); //set your function for unlink and call it on the delete route function removeImage(image){ fs.unlink(uploadPath, image), (err)=>{ if(err) console.log(err) }) }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter