Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Delete/Unlink code in Section 3/Storing Data/lib/data.js #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions Section 3/Storing Data/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,14 @@ lib.update = function(dir,file,data,callback){

// Delete a file
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 deleting file');
}
});


};

// Export the module
Expand Down