Ignore headers of csv files with papaparse

nardjes

NardjesBen

Posted on June 27, 2021

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?

💖 💪 🙅 🚩
nardjes
NardjesBen

Posted on June 27, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related