Home > Blockchain >  Is there a "start" like "end" in fs.createreadstream?
Is there a "start" like "end" in fs.createreadstream?

Time:09-22

I am using csv-parser library, and i want to check table captions before parsing them

const fs = require('fs')
const csv = require('csv-parser')

fs.createReadStream('tes.csv')
        .pipe(csv())
        .on('start', start=>{
            console.log(start)
//here i should check how column names are captioned
        })
        .on('data', data=>{
            console.log(data)
//if they are captioned as requiered i do things
        })

I checked the documentation but didnt find anything about it, so there is something to use like .on('end', ()=>{})? Or there is an other way to get column names?

CodePudding user response:

You can get column names with headers event. csv-parser emit headers event after header row parsed. First parameter of callback function is Array[String] and you can access column names or headers . (more doc)

const fs = require('fs')
const csv = require('csv-parser')

fs.createReadStream('tes.csv')
        .pipe(csv())
        .on('headers', headers=>{
            console.log(headers)
        })
        .on('data', data=>{
            console.log(data)
        })
  • Related