Home > Enterprise >  How to get data for fs.createReadStream
How to get data for fs.createReadStream

Time:10-28

I am trying to get data from a csv file but I am getting the fs.readStream object instead

const fs = require("fs");
const csv = require("csv-parser");
const path = require("path");

const file = path.resolve(__dirname, "./file");

export const parseCsv = async () => {
  const result = [];

  return await fs
    .createReadStream(file)
    .pipe(csv())
    .on("data", (data) => {
      result.push(data);
    })
    .on("end", () => {
      const obj = {};
      result.forEach((service) => {
        if (!obj[service.code]) {
          obj[service.code] = service.rate;
        }
      });
      console.log(obj);
      return obj; // looking to get this
    });
};

I'm am creating an object map of the csv file and looking to get that data but I keep getting this instead.

CsvParser {
  _readableState: ReadableState {
    objectMode: true,
    highWaterMark: 16,
    buffer: BufferList { head: null, tail: null, length: 0 },
    length: 0,
    pipes: [],
    flowing: true,
    ended: false,
    endEmitted: false,
    reading: false,
    constructed: true,
    sync: false,
    needReadable: false,
    emittedReadable: false,
    readableListening: false,
    resumeScheduled: true,
    etc...

CodePudding user response:

await only does something useful when you await a promise and fs.createReadStream().pipe().on() returns a stream, not a promise. I would suggest you wrap this stream code in a promise where you can then resolve() and reject() the promise appropriately.

Then, the caller will use await or .then() on the returned promise to get access to the final result.

const fs = require("fs");
const csv = require("csv-parser");
const path = require("path");

const file = path.resolve(__dirname, "./file");

export const parseCsv = () => {
    const result = [];
    return new Promise((resolve, reject) => {
        fs.createReadStream(file)
            .pipe(csv())
            .on("data", (data) => {
                result.push(data);
            }).on("end", () => {
                const obj = {};
                result.forEach((service) => {
                    if (!obj[service.code]) {
                        obj[service.code] = service.rate;
                    }
                });
                console.log(obj);
                resolve(obj);
            }).on(error, reject);
    });
}
  • Related