mafintosh / csv-parser

Streaming csv parser inspired by binary-csv that aims to be faster than everyone else
MIT License
1.41k stars 134 forks source link

Row value cannot be accessed at expected key despite row object having expected keys and values #237

Open Otomakan opened 3 weeks ago

Otomakan commented 3 weeks ago

Expected Behavior

Logs the content of the object at the specified key. EG: console.log(row['siteName'] ) would log "Your site" or "My site"

Actual Behavior

Doesn't extract the value of the object at the given key. The value at the column "siteName" can only be extracted using Object.keys(row)[0] 0 being the index of the first column.

How Do We Reproduce?

Here is the data siteName July August
Your site 323 444
My site 111 222
const fs = require('fs');
const csv = require('csv-parser');

const file1 = './data.csv';
let totalGenerationData = {};

// Function to read CSV and extract the column data based on siteName
function readCSV(filePath, columnName, dataObject, callback) {
    fs.createReadStream(filePath)
        .pipe(csv())
        .on('data', (row) => {

            // As expected logs : 
            // { 'siteName': 'My site', July: '28926', August: '37746' }
            // { 'siteName': 'Your site', July: '7107', August: '9352' }
            console.log(row)

            //As expected logs  "siteName" "siteName"
            console.log(Object.keys(row)[0])

            // Logs undefined undefined
            console.log(row['siteName'])

            // Logs "My site"
            // Logs "Your site"
            console.log(row[Object.keys(row)[0]])
        })
}

// Read both files and compare the data
readCSV(file1, '', totalGenerationData, () => { });

Interestingly the bug cannot be reproduced if siteName is the last column of the csv file.

Otomakan commented 3 weeks ago

It strangely seems like it could be a Node bug and not csv-parser , except if there is some sort of unlikely delayed side effect.

chrp commented 2 weeks ago

Did you check that your file does not contain invisible characters in the header line? Try hexdump -C data.csv. Typical candidate migth be a BOM which adds 3 invisible chars at the beginning of the file.