Node.js fs.writeFile() empties the file

后端 未结 3 1750
自闭症患者
自闭症患者 2021-01-15 05:56

I have an update method which gets called about every 16-40ms, and inside I have this code:

this.fs.writeFile(\"./data.json\", JSON.stringify({
    totalPlay         


        
3条回答
  •  终归单人心
    2021-01-15 07:00

    if the error is caused due to bad input (the data you want to write) then make sure the data is as they should and then do the writeFile. if the error is caused due to failure of the writeFile even though the input is Ok, you could check that the function is executed until the file is written. One way is using the async doWhilst function.

    async.doWhilst(
        writeFile(), //your function here but instead of err when fail callback success to loop again
        check_if_file_null, //a function that checks that the file is not null
        function (err) {
            //here the file is not null
        }
    );
    

提交回复
热议问题