Wednesday, 3 May 2017

Node writeFile not writing the whole file

I am trying to read a csv file and write the contents to a new csv file but am having a few issues in doing so.

My code seems to be reading the file ok and if i log the results of body in the console it shows the complete file. However when I try and write to the file, it does not seem to write all the contents.

The code I am using is as follows...

var http = require('http');
var fs = require('fs');
var readfile = "http://datafeed.api.productserve.com/datafeed.csv";
var writefile = "app/data/file.csv";

http.get(readfile, function(response) {
    var body = '';
    response.on("data", function(chunk) {
        body += chunk;
    });
    response.on('end', function(){
        fs.writeFile(writefile, body, function(error) {
            if(error) {
                console.error("write error: " + error.message);
            } else {
                console.log('Successful');
            }
        });
    });
});

Am I doing something incorrect? Or is there some sort of limit on how much you can write?

Any help is much appreciated :)



via Phil

No comments:

Post a Comment