I have a list of csv-files and want to parse them. For this I imported fast-csv. I tried to implement the functionality as described in another stack overflow thread.
But in my code it does not work, and I am really confused why it does not work.
I always get an empty array as output for log(data)
.
import { ParserOptionsArgs, parseFile } from '@fast-csv/parse'
import fs from 'fs'
import path from 'path'
var files: string[] = []
fs.readdirSync(inArg).forEach((file) => {
files.push(path.parse(file).name)
})
files.forEach( async(file) => {
const data: any = await readCsv(`${inArg}/${file}.csv`, { headers: true }, (row) => {})
log(data)
// here I want make some useful with the data
})
function readCsv(path: string, options: ParserOptionsArgs | undefined, rowProcessor: (arg0: any) => any) {
return new Promise((resolve) => {
const data: any = []
parseFile(path, options)
.on('data', (row: any) => {
const obj = rowProcessor(row)
if (obj) {
data.push(obj)
log(obj)
}
})
.on('end', () => {
resolve(data)
log('Done')
})
})
}
The console output:
Files: [ 'test1', 'test2', 'test3', 'test4' ]
Done
[]
Done
[]
Done
[]
Done
[]
May someone can help me out?