From a329c4f68ca3e581b12e414fd3e869d1a9e5271f Mon Sep 17 00:00:00 2001 From: pogl8 <72755075+pogl8@users.noreply.github.com> Date: Mon, 24 May 2021 18:58:45 -0300 Subject: [PATCH] Fixing delete function The previous code seems the be a copy of the 'update' function. I'm just sending the 'delete' function that is in the course. Thanks a lot! It's by far the best js course i have had so far, regardless of being Node or not. Cheers --- Section 3/Storing Data/lib/data.js | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/Section 3/Storing Data/lib/data.js b/Section 3/Storing Data/lib/data.js index 0b37069..dc0aaad 100644 --- a/Section 3/Storing Data/lib/data.js +++ b/Section 3/Storing Data/lib/data.js @@ -87,34 +87,16 @@ lib.update = function(dir,file,data,callback){ }; // Delete a file -lib.delete = function(dir,file,callback){ +lib.delete = function (dir, file, callback) { - // Open the file for writing - fs.unlink(lib.baseDir+dir+'/'+file+'.json', 'r+', function(err, fileDescriptor){ - if(!err && fileDescriptor){ - // Convert data to string - var stringData = JSON.stringify(data); - - // Write to file and close it - fs.writeFile(fileDescriptor, stringData,function(err){ - if(!err){ - fs.close(fileDescriptor,function(err){ - if(!err){ - callback(false); - } else { - callback('Error closing existing file'); - } - }); - } else { - callback('Error writing to existing file'); - } - }); + // Unlink the file + fs.unlink(lib.baseDir + dir + '/' + file + '.json', function (err) { + if (!err) { + callback(false); } else { - callback('Could not open file for updating, it may not exist yet'); + callback('Error closing existing file'); } }); - - }; // Export the module