Wednesday, 19 April 2017

fs.readFile() not read the whole files and everytime the read contents missing few parts

I just met a strange problem of fs.readFile() in my program. The file is not big(only 500k in size), it is a JSON.stringify(obj) string. I am trying to read it out and pass it to the front-end. While in the callback of fs.readFile("/path/filename", "utf8", function(e,data){}).I only got in-complete string.

    fs.readFile("/path/filename",'utf8', function (err,data) {
            if(err) {
               throw err;
            } else {
               let str = JSON.parse(data); // it will throw error by saying "SyntaxError: Unexpected end of JSON input"
            }
        }); 

Have you met this problem? What's the solution to read the whole/complete file?



via Robin

No comments:

Post a Comment