Skip to content

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

@Otomakan

Description

@Otomakan
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions