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.

1 Answer 1

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();
})();
Sign up to request clarification or add additional context in comments.

3 Comments

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??
Honestly I don't know about fast-csv, but according to the docs you to specify an options-object allowing the following settings: 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.
I see. I will give it a try! Thanks for your help, I really appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.