My code is working correctly, I wanted to add some error checking to make sure no object with empty fields could be inserted into the DB. I am having some trouble getting that part working right. The data is coming from a local csv file like so:
TempCSV.csv
name, address, ID, contactInfo
bob, 214 elm, 123, email
joe, 817 beach, 321, email
,45 hollywood,456,
The first two lines should be inserted but not the last one because it is missing name, which is a required field. So basically each field needs to be checked while it is parsed to see if it is empty or not.
Here is my code dbparser.js
const mongoose = require("mongoose");
const passport = require("passport");
const csvtojson = require("csvtojson");
const router = require("express").Router();
const userSchema= new mongoose.Schema({
name: String,
address: String,
ID: String,
contactInfo: String
});
const userModel= mongoose.model("User");
const async = require('async');
csvtojson()
.fromFile("TempCSV.csv")
.then(csvData => {
async.eachSeries(csvData,(data,callback) => {
let entity = {
name: data.name,
address: data.owner,
ID: data.ownerID,
};
userModel.create(entity, function(err)
{
if(err) return callback(err);
return callback(null);
})
},
(err) => {
if(err) console.log(err);
console.log("users are successfully imported!!!");
});
});
I thought adding the const userSchema would prevent it from adding empty fields but it did not. Any help would be appreciated.