最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get data for fs.createReadStream - Stack Overflow

programmeradmin3浏览0评论

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...

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...
Share Improve this question asked Oct 26, 2022 at 21:30 Michael SanfordMichael Sanford 1171 gold badge1 silver badge12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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);
    });
}
发布评论

评论列表(0)

  1. 暂无评论