mafintosh / mafintosh/csv-parser

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

Open
#237 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.5k
Forks
143
PR merge metrics
No merged PRs in 30d

Description

* 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided data.csv example and the readCSV snippet, then inspect how csv-parser constructs row keys for the first column. Reproduce the reported difference between the first and last column cases and run the project’s existing tests; done means row['siteName'] returns the expected value for both positions without breaking other columns.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.