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

callback vs callBack in node.js #26

Open
dcjayasuriya2020 opened this issue Jan 24, 2021 · 1 comment
Open

callback vs callBack in node.js #26

dcjayasuriya2020 opened this issue Jan 24, 2021 · 1 comment

Comments

@dcjayasuriya2020
Copy link

how complete the ftruncate ? with/withtout callback(false)?
my code in RESTful API is :

lib.update=function(dir,file,data, callBack){
//Open the file for writing
fs.open(lib.baseDir+dir+'/'+file+'.json', 'r+',function(err,fileDescriptor){
    if(!err && fileDescriptor){
        var stringData = JSON.stringify(data);
        
        fs.ftruncate(fileDescriptor, (err) => {
                if (!err) {
                    fs.writeFile(fileDescriptor, stringData, function (err) {
                        if (!err) {
                            fs.close(fileDescriptor, function (err) {
                                if (!err) {
                                    callBack(true);
                                } else {
                                    callback('Err in closing existing file');
                                }
                            });
                        } else {
                            callback('Error updating to existing file');
                        }
                    });

                } else {
                    callback('Err truncating file');
                }
            });
    } else{
        callBack('Cannot Update the file, it may not exist yet');
    }
});
};

the error is actually, when we run the postman, it is keep sending the 'sending the request' but never ending; but the file is updated

Oh! My goodness !!! I just noticed
have u notice I have written "callback" as "callBack" in 2 place.
and I jst made it as "callback" instead of any caps... after editing 'callback' in fs.open (bottom else for err msg of 'file may not exist yet'), then postman works, file updated. DONE!

Then when I also change the 'callBack' in fs.close (if no err), then it again sending the reuqest, but not even updating...?

Can someone explain the logic here?
Is this matter of NodeJs version ?
Food For Learning

@leskeylevy
Copy link

I am having a similar problem 'callback is not defined' ... I tried changing it to console.log('false') it logs false and keeps running even after file is updated. What am I missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants