Thursday, 20 April 2017

SetTimeout + fs.writeFile erases my object

I have an object "rooms" and I'am trying to print it's content into a file "logging.json" every 10 seconds.

setTimeout(function(){
  fs.writeFile('./logging.json',JSON.stringify(rooms,null,'\t'),function(err) {
    if(err) return console.error(err);
    console.log('done');
  });
}, 10000);

The first time (after 10 seconds) it works and I got my file logging.json completed. But the second time that the function is called, the content of my object "rooms" is erased and the logging.json file that I obtain is also empty.

Also, the rest of my program works correctly withaout this portion of code so I don't think it's coming from something else.

Could you tell please my why I'm getting this and how to fix it?



via Mit

No comments:

Post a Comment