I am trying to parse a csv file with fast-csv and at the end store all the lines in a database with sequelize
Here is my script to parse the data:
module.exports = async () => {
try {
let csvData = [];
csv
.parseFile(__basedir + "/test.csv", { headers: true })
.on('data', (data) => {
csvData.push(data);
})
.on('end', () => {
Card_Report.bulkCreate(csvData)
});
} catch (e) {
console.error('Something went wrong', e)
}
}
Here is my table model:
const Test= sequelize.define(
'test',
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: uuidv4(),
allowNull: false
},
name: {
type: DataTypes.STRING,
},
age: {
type: DataTypes.INTEGER,
}
}, {
createdAt: false,
updatedAt: false,
tableName: 'test'
}
)
return Test
}
and here is my csv file :
name, age
nina, 24
sarah, 23
when I run the script I get this error :
Unhandled rejection SequelizeUniqueConstraintError: Validation error
But when there is only one row in my csv file it adds the row to the table correctly
How can I fix this?