2

I am attempting to parse a csv-file using fast-csv library and convert each values number or string to create 2d array. But I can't return array in ReadStream. Can you give me advice on my code?

const fs = require("fs");
const csv = require("fast-csv");

async csvParse(){

  let array = [];
  
  options = {
    map(value){
      // convert value's column type to number or string
    }
  }

  fs.createReadStream(csvfile)
      .pipe(csv.parse(options))
      .on("error", error => console.log(error))
      .on("data", data => array.push(data))
      .on("end", function(){
         console.log(array);
         return array;
      })
}

input

name,date,score,id
John,2020-01-01,1000,10

expected-output

[["name", "date", "score", "id"], ["John", "2020-01-01", 1000, 10]]

actual-output

// no output

I expect the code to return "expected-output" that converted from "input", but it returns none. I understand this is due to me attempting to return a value from the anonymous function on(), however I do not know the proper approach.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
Pirikara
  • 343
  • 3
  • 12

1 Answers1

1

You can wrap processing of the file-stream and the csv-conversion in a promise and resolve the promise once the end-event is emitted:

const fs = require("fs");
const csv = require("fast-csv");

function csvParse() {

    const options = {
        // your options here
    }
    let array = [];
    return new Promise((resolve, reject) => {
        fs.createReadStream(csvfile)
        .pipe(csv.parse(options))
        .on("error", error => reject(error))
        .on("data", data => array.push(data))
        .on("end", () => {
            console.log(array);
            resolve(array);
        });
    });

}

Then call it like this:

(async() => {
    const result = await csvParse();
})();
eol
  • 23,236
  • 5
  • 46
  • 64
  • Thank you! Im able to return array from this method! But, map of "options" does not work......so, I cannot convert the type of value to string or number..... My use of options is wrong?? – Pirikara Jun 28 '20 at 16:21
  • Honestly I don't know about `fast-csv`, but according to the docs you to specify an options-object allowing the following settings: https://c2fo.io/fast-csv/docs/parsing/options/ So if you want to ignore empty values for example you would set the object to `{ ignoreEmpty: true}`, there's no ootb option for converting string to number unfortunately. – eol Jun 28 '20 at 16:48
  • I see. I will give it a try! Thanks for your help, I really appreciate it! – Pirikara Jun 29 '20 at 01:17