0

I have a case where once user download a file i am deleting it from the directory, Now once new message comes in its still trying to append to the file that does not exist , How can i check if obj.file exist then only append ?

server.js

recordLogs: function (obj) {
    var path = './app/records/templogs'
    var fileAppend = path + '/'+ obj.file;
    console.log('data from recoring', obj.data);
    fs.readdir(path, function(err, items) {
        items.forEach(function(file){
            if(obj.file === file){
                fs.appendFile(fileAppend, obj.data, null, 'utf8', function (err) {
                    if (err) throw err;
                });
                console.log('filename in records',obj.file);
            }
        });
    });
}
4
  • Possible duplicate of Node.js check exist file Commented Aug 1, 2016 at 13:50
  • It looks like what you're asking for is already accomplished by your code. What problem are you encountering? Commented Aug 1, 2016 at 13:57
  • As i mentioned in my question when file is deleted , its still trying to append. Commented Aug 1, 2016 at 14:01
  • did you try using fs.stat(fileName, cb) ? Commented Aug 4, 2016 at 13:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.