Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Otomakan opened this issue Jun 11, 2024 · 2 comments

Comments

@Otomakan
Copy link

  • Operating System: MacOS BigSur 11.4
  • Node Version: 20.9.0 & 16.17.0
  • NPM Version: 10.1.0
  • csv-parser Version: 3.0.0

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
Copy link
Author

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
Copy link

chrp commented Jun 12, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants