fudge
fudge

Reputation: 311

Nodejs - importing a CSV, keeps turning into "buffer"

I've got a script that imports a csv to an array. It works fine when I don't add any options and use a regular csv file, but once I mix it up things start to go awry:

var fs = require('fs');
var csv = require('fast-csv');
var counter = 0;

// var csvData = [];
let csvStream = csv.fromPath("C:\\A_projects\\3-23-2018 Twillio testing\\test\\test.txt",delimiter="\t",headers=true)
  .on("data", function(record){
      csvStream.pause();

      if(counter < 10)
      {
          let acct_id = record.acct_id;
          let acct_id2 = record.acct_id2;
          let address = record.address;
          let date = record.date;
          let appt_time = record.appt_time;
          let name = record.name;
          let num1 = record.num1;
          let num2 = record.num2;
          let num3 = record.num3;
      }
      console.log(record);
      ++counter;
      csvStream.resume();
  }).on("end", function(){
      console.log("Job is done!!");
  }).on("error", function(){
      console.log(err);
  });

If I remove the delimiter and headers options it spits out each row as an array, but when I add either or both options it is now a "buffer"

Buffer(71) [91, 34, 97, 99, 99, 116, 95, 105, …]

I've even tried adding the option of "objectMode=true" which should convert any buffer back into a string and it still comes out as a buffer. Oddly enough, using that as the only options makes it into a buffer, regardless if it's true or false.

First off, how can I go about fixing this? Second, it should not be a buffer in the first place, but if I have to deal with this, is there a reasonable way to convert what I assume is plain data back into a string?

Upvotes: 0

Views: 1064

Answers (1)

fudge
fudge

Reputation: 311

I just realized what I was doing wrong. My syntax for options was incorrect:

let csvStream = csv.fromPath("C:\\x\\test.txt",objectMode=true)

Should have been like below:

let csvStream = csv.fromPath("C:\\x\\test.txt", { objectMode:true } )

Upvotes: 1

Related Questions