While deleting FIles in our Node application we sometimes use fire and forget that means we are not checking it back weather our file is deleted or not
To use that
We need to make a file or we can simply use
const fs = require('fs'); const deleteFile = (filePath) => { fs.unlink(filePath, (err) => { if (err) { throw (err); } }) } exports.deleFile = deleteFile;
We are exporting because As a node developer we always need to make our application in a systematic way. So
make a file in the util folder where use this code then
where we have to use this is where we want to delete our image or file which is not usefull now so in our case edit product we have to write
const fileHelper = require('../util/file');
in the post edit page use this
fileHelper.deleteFile(product.imageUrl);
If we want to see the result with that then use the callbacks like this
We are firing this function and We don't care about the result. If we wanted to, we would have to pass my callback here, so a function which then continues with the rest.
fileHelper.deleteFile(product.imageUrl, (err){
throw (err);
);
Comments