node.jsmongodbcsvmongoosecsvtojson

Mongoose import data from csv only inserting partial data


I am new to MongoDB and Node.js and I am facing an issue while saving data into MongoDB Atlas from a .csv file through mongoose.

I want the .csv file to be uploaded by a user through file input and then insert the data in this .csv to MongoDB Atlas. For this, I made it so that the .csv file is converted first into JSON format by using the 'csvtojson' module. But out of 7 rows of data, only 4 rows are getting saved on MongoDB Atlas. Also, I am get the following errors:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (D:\Projects\node_modules\express\lib\response.js:771:10)
    at ServerResponse.send (D:\Projects\node_modules\express\lib\response.js:170:12)
    at done (D:\Projects\node_modules\express\lib\response.js:1008:10)
    at tryHandleCache (D:\Projects\node_modules\ejs\lib\ejs.js:278:5)
    at View.exports.renderFile [as engine] (D:\Projects\node_modules\ejs\lib\ejs.js:489:10)
    at View.render (D:\Projects\node_modules\express\lib\view.js:135:8)
    at tryRender (D:\Projects\node_modules\express\lib\application.js:640:10)
    at Function.render (D:\Projects\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (D:\Projects\node_modules\express\lib\response.js:1012:7)
    at D:\Projects\temp\file.js:57:21
    at D:\Projects\node_modules\mongoose\lib\model.js:5068:18
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
events.js:352
      throw er; // Unhandled 'error' event
      ^

TypeError: req.next is not a function
    at done (D:\Projects\node_modules\express\lib\response.js:1007:25)
    at tryRender (D:\Projects\node_modules\express\lib\application.js:642:5)
    at Function.render (D:\Projects\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (D:\Projects\node_modules\express\lib\response.js:1012:7)
    at D:\Projects\temp\file.js:57:21
    at D:\Projects\node_modules\mongoose\lib\model.js:5068:18
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
Emitted 'error' event on Function instance at:
    at D:\Projects\node_modules\mongoose\lib\model.js:5070:15
    at processTicksAndRejections (internal/process/task_queues.js:77:11)

Here is the code:

const csvtojson = require("csvtojson");
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const Student = require("./models/student");
const encrypt = require("mongoose-encryption")
//const fs = require("fs");

const app = express();

app.use(express.static("public"));
app.set("views", "./views");
app.set("view engine", "ejs");
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
);

try {
  mongoose.connect(
    process.env.URL,
    { useNewUrlParser: true, useUnifiedTopology: true },
    () => console.log("Mongoose connected")
  );
} catch (e) {
  console.log("Failed to connect to mongoose!");
}

app.get("/test", function (req, res) {
  res.render("test");
});

app.post("/test", async function (req, res) {
  const csvfilepath = await req.body.file;
   await csvtojson().fromFile(csvfilepath).then((json) => {
      var i;
      for (i = 0; i < json.length; i++) {
        console.log(json[i].fname)     //console.log prints all the entries
        
        const newStudent = new Student({
            fname: json[i].fname,
            lname: json[i].nlame,
            prn: json[i].prn,
            username: json[i].email,
            password: json[i].password,
            department: json[i].department,
            year: json[i].year,
            batch: json[i].batch,
            placement_Status: json[i].placement_Status
        })
        newStudent.save(function(err){
            if(err){
                console.log(err);
            }else{
                res.render("test")
            }
        })
      }
    });
});

app.listen(3000, function () {
    console.log("Server started on port 3000...");
  });

Here's the snap of the csv file

MongoDB Atlas

I would really appreciate if someone suggests me a fix or an alternative for this code.

Thanks in advance!


Solution

  • The error was in this else statement of this particular block of code:

    newStudent.save(function(err){
                if(err){
                    console.log(err);
                }else{
                    res.render("test")
                }
    

    Replacing the above code with the following fixes the code. And the redirect/render function can be written outside the for loop.

    newStudent.save(function(err){
                    if(err){
                        console.log(err);
                    }else{
                        console.log(success);
                    }