node-csvtojson icon indicating copy to clipboard operation
node-csvtojson copied to clipboard

Version 0.4.4 broke existing code

Open jsgandalf opened this issue 11 years ago • 6 comments

Thanks for adding the error event Listener from 0.4.3 to 0.4.4

However, my code stopped working when I upgraded to this new version. What I mean by that is whenever I upload a csv, my code gets stuck now.

Current code, for reference:

var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
 console.log(files.file.type);
 if (!files || !files.file || files.file.size == '0') {
   res.send("CSV must not be empty.");
 }else if( files.file.type == 'text/csv'){
   var Converter = require("csvtojson").Converter;

  var csvFileName = files.file.path;
  var fileStream = fs.createReadStream(csvFileName);
  //new converter instance

  var csvConverter = new Converter({constructResult:true});

  csvConverter.on("record_parsed",function(resultRow,rawRow,rowIndex) {
    //console.log(resultRow); //here is your result json object
    fileStream.unpipe(csvConverter);
    console.log('manually close the file stream2');
    csvConverter.end();
  });

  //end_parsed will be emitted once parsing finished
  csvConverter.on("end_parsed",function(jsonObj) {
    res.json(200, jsonObj);
  });

  //read from file
  fileStream.pipe(csvConverter, { end: false });
} else {
  res.send("Not a valid type! Must be CSV format");
}
});

jsgandalf avatar Nov 03 '15 07:11 jsgandalf

In your code you don't need subscribe record_parsed event nor call converter.end explicitly. Remove the event listener and try again. Also can you share the csv file you have problem with using something like fileota.com ?

On Tuesday, 3 November 2015, Sean [email protected] wrote:

Thanks for adding the error event Listener from 0.4.3 to 0.4.4

However, my code stopped working when I upgraded to this new version. What I mean by that is whenever I upload a csv, my code gets stuck now.

Current code, for reference:

var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { console.log(files.file.type); if (!files || !files.file || files.file.size == '0') { res.send("CSV must not be empty."); }else if( files.file.type == 'text/csv'){ var Converter = require("csvtojson").Converter;

var csvFileName = files.file.path; var fileStream = fs.createReadStream(csvFileName); //new converter instance

var csvConverter = new Converter({constructResult:true});

csvConverter.on("record_parsed",function(resultRow,rawRow,rowIndex) { //console.log(resultRow); //here is your result json object fileStream.unpipe(csvConverter); console.log('manually close the file stream2'); csvConverter.end(); });

//end_parsed will be emitted once parsing finished csvConverter.on("end_parsed",function(jsonObj) { res.json(200, jsonObj); });

//read from file fileStream.pipe(csvConverter, { end: false }); } else { res.send("Not a valid type! Must be CSV format"); } });

— Reply to this email directly or view it on GitHub https://github.com/Keyang/node-csvtojson/issues/59.

Keyang avatar Nov 03 '15 08:11 Keyang

Here is my csv: http://fileota.com/s/UWzUO

This is off topic, but... I close the stream early because I only need the headers of this csv. If it happens to be a very large csv... I don't want to parse the whole thing, just get the headers in this case. This is so the user on the client can match headers with database values before the actual import.

Here is my revised code with the same problem... I've also taken out the record_parsed event call.

var formidable = require('formidable');
var fs = require("fs");
exports.import = function(req, res){
  var form = new formidable.IncomingForm();
  form.parse(req, function(err, fields, files) {
    if (!files || !files.file || files.file.size == '0') {
       res.send("CSV must not be empty.");
    }else if( files.file.type == 'text/csv'){
       var Converter = require("csvtojson").Converter;
       var fileStream = fs.createReadStream(files.file.path);
       var csvConverter = new Converter({constructResult:false});

      //end_parsed will be emitted once parsing finished
      csvConverter.on("end_parsed",function(jsonObj) {
        return res.json(200, {});
      });
      //read from file
      fileStream.pipe(csvConverter);
  } else {
   res.send("Not a valid type! Must be CSV format");
  }
 });
}

Any idea's why node is choking? Thanks in advance

jsgandalf avatar Nov 03 '15 14:11 jsgandalf

Hi, I cannot reproduce this problem with the sample data. All entries have gone through. Also converter.end() will not stop emitting "record_parsed" event if there are more than one line csv row in buffer (memory). You would better close read stream as well. Are you running the node process under debugging mode? If true, I l accidentally left a "debugger;" statement in the code base and I have removed it in 0.4.5

~Keyang

On 3 November 2015 at 14:48, Sean [email protected] wrote:

Here is my csv: http://fileota.com/s/UWzUO

This is off topic, but... I close the stream early because I only need the headers of this csv. If it happens to be a very large csv... I don't want to parse the whole thing, just get the headers in this case. This is so the user on the client can match headers with database values before the actual import.

Here is my revised code with the same problem... I've also taken out the record_parsed event call.

exports.import = function(req, res){ var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { if (!files || !files.file || files.file.size == '0') { res.send("CSV must not be empty."); }else if( files.file.type == 'text/csv'){ var Converter = require("csvtojson").Converter; var fileStream = fs.createReadStream(files.file.path); var csvConverter = new Converter({constructResult:false});

  //end_parsed will be emitted once parsing finished
  csvConverter.on("end_parsed",function(jsonObj) {
    return res.json(200, {});
  });
  //read from file
  fileStream.pipe(csvConverter);

} else { res.send("Not a valid type! Must be CSV format"); } }); }

Any idea's why node is choking? Thanks in advance

— Reply to this email directly or view it on GitHub https://github.com/Keyang/node-csvtojson/issues/59#issuecomment-153376602 .

Keyang avatar Nov 03 '15 15:11 Keyang

Thanks for the tip on closing the read stream... I had forgotten about that.

I will create a new repo and share with you on runnable.io.

I really like this plugin you've created... and the error reporting. Very powerful.

On Tue, Nov 3, 2015 at 8:47 AM, Keyang Xiang [email protected] wrote:

Hi, I cannot reproduce this problem with the sample data. All entries have gone through. Also converter.end() will not stop emitting "record_parsed" event if there are more than one line csv row in buffer (memory). You would better close read stream as well. Are you running the node process under debugging mode? If true, I l accidentally left a "debugger;" statement in the code base and I have removed it in 0.4.5

~Keyang

On 3 November 2015 at 14:48, Sean [email protected] wrote:

Here is my csv: http://fileota.com/s/UWzUO

This is off topic, but... I close the stream early because I only need the headers of this csv. If it happens to be a very large csv... I don't want to parse the whole thing, just get the headers in this case. This is so the user on the client can match headers with database values before the actual import.

Here is my revised code with the same problem... I've also taken out the record_parsed event call.

exports.import = function(req, res){ var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { if (!files || !files.file || files.file.size == '0') { res.send("CSV must not be empty."); }else if( files.file.type == 'text/csv'){ var Converter = require("csvtojson").Converter; var fileStream = fs.createReadStream(files.file.path); var csvConverter = new Converter({constructResult:false});

//end_parsed will be emitted once parsing finished csvConverter.on("end_parsed",function(jsonObj) { return res.json(200, {}); }); //read from file fileStream.pipe(csvConverter); } else { res.send("Not a valid type! Must be CSV format"); } }); }

Any idea's why node is choking? Thanks in advance

— Reply to this email directly or view it on GitHub < https://github.com/Keyang/node-csvtojson/issues/59#issuecomment-153376602>

.

— Reply to this email directly or view it on GitHub https://github.com/Keyang/node-csvtojson/issues/59#issuecomment-153394510 .

jsgandalf avatar Nov 03 '15 17:11 jsgandalf

No Problem. Please let me know if you have created something re-producible.

~Keyang

Keyang avatar Nov 03 '15 23:11 Keyang

Close due to lack of activity.

Keyang avatar Nov 16 '15 07:11 Keyang