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