2

I am running papa parse on the server to read and parse a csv file usign the following code:

function getData() {
    console.log("Started getData...");
    const file = fs.createReadStream(filePath);
    papa.parse(file, {
        header: true,
        complete: createRecord
    });
}

My complete callback function looks like this:

function createRecord(row, file) {
    records = row.data;
    console.log("Started createRecord...");
    records.forEach((record) => {
        console.log(record)
        Activity.create(record);
    });
}

I then write this to a MondoDB database. Everything works perfectly well, except for the first field name that is parsed by papa. The record output from the console.log(record) call above looks like this:

{
  'Activity_Date': '2018-08-28',
  Time_Start: '2018-08-28 20:20',
  Time_End: '2018-08-28 21:01',
  Duration: 0.027962963,
  Subject: 'CS410',
  Semester: 'Fall 2018',
  Week: 1,
  Task: 'Lectures',
  Day: 2
}

Notice the quotes around Activity_Date. This causes Mongo to ignore the first field and only commit the remainder to the database.

Attempting to edit the csv file differently has not resulted in anything useful. What is weird is that if I look at the meta data from papa parse, it gives me the field names without the qoutes.

The Mongo Schema for Activity looks like this:

const mongoose = require("mongoose");

const activitySchema = new mongoose.Schema({
    Activity_Date: String,
    Time_Start: String,
    Time_End: String,
    Duration: Number,
    Subject: String,
    Semester: String,
    Week: Number,
    Task: String,
    Day: Number
});

module.exports = mongoose.model("Activity", activitySchema)

Any assistance in getting rid of the quotes will be very welcome!

Thanks

Paul
  • 813
  • 11
  • 27

1 Answers1

7

It has been a while since asked but i will leave what worked for me. Try using this in the config object:

transformHeader: h => h.trim()

aguparamio
  • 71
  • 1
  • 3
  • This worked for me almost. What I got was ' "Id"' for the first field. So I used `transformHeader: h => h.trim().replace(/"/g, '')` Of course this will remove ALL double quotes from the headers, but this is fine with me as I don't want double quotes in my field names anyway. – ksh Mar 18 '22 at 13:57