Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

node.js - callback or callBack? complete the ftruncate with callback?

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

whilst typeing on stackoverflow, I just noticed Oh! My goodness !!! 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

question from:https://stackoverflow.com/questions/65866087/callback-or-callback-complete-the-ftruncate-with-callback

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...