0

我是 MongoDB 和 Node.js 的新手,在通过 mongoose 从 .csv 文件将数据保存到 MongoDB Atlas 时遇到问题。

我希望用户通过文件输入上传 .csv 文件,然后将此 .csv 中的数据插入到 MongoDB Atlas。为此,我使用“csvtojson”模块将 .csv 文件首先转换为 JSON 格式。但是在 7 行数据中,只有 4 行保存在 MongoDB Atlas 上。另外,我收到以下错误:

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)

这是代码:

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...");
  });

这是csv文件的快照

MongoDB地图集

如果有人建议我对此代码进行修复或替代,我将不胜感激。

提前致谢!

4

1 回答 1

0

错误出现在此特定代码块的 else 语句中:

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

用以下代码替换上面的代码可以修复代码。并且重定向/渲染函数可以写在 for 循环之外。

newStudent.save(function(err){
                if(err){
                    console.log(err);
                }else{
                    console.log(success);
                }
于 2021-08-13T09:02:35.080 回答