DEV Community

NardjesBen
NardjesBen

Posted on

5 3

Ignore headers of csv files with papaparse

I have a csv file that I need to parse and store in a database. I use sequelize for this

Here is how I defined the model of my table in sequelize :

    const Person= sequelize.define(
      'person',
      {
        id: {
          type: DataTypes.UUID,
          primaryKey: true,
          defaultValue: uuidv4(),
          allowNull: false
        },
        name: {
          type: DataTypes.STRING,
        },
        age: {
          type: DataTypes.INTEGER,
        }
        }, {
            createdAt: false,
            updatedAt: false,        
            tableName: 'person'
         }
    )
    return Person
  }
Enter fullscreen mode Exit fullscreen mode

then I have a csv file "test.csv":

he ,dd
liza, 23
sarah, 22
Enter fullscreen mode Exit fullscreen mode

basically I can't change the headers in my csv file and I have to keep the same structure defined in the db, I use papaparse to parse this file :

        const papa = require('papaparse');
        const fs = require('fs')

        const file = fs.createReadStream(__basedir + "/test.csv");


        papa.parse(file, {
          header: true,
          step: async function(results, parser) {
            console.log("Row data:", results.data);
            try{
              await Person.create(results.data)

            }catch(e){
              console.log(e)
            }

          }
      });
Enter fullscreen mode Exit fullscreen mode

in output I get the following error :

SequelizeUniqueConstraintError: Validation error
Enter fullscreen mode Exit fullscreen mode

the name, surname and age fields are null in the database

I am looking for a solution to ignore the headers of the csv file, how can I do that?

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (1)

Collapse
 
ganeshmani profile image
GaneshMani

try setting header: false while parsing.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more