Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 当标题为true时,fast csv返回未定义的数据_Node.js_Csv_Express_Mongoose - Fatal编程技术网

Node.js 当标题为true时,fast csv返回未定义的数据

Node.js 当标题为true时,fast csv返回未定义的数据,node.js,csv,express,mongoose,Node.js,Csv,Express,Mongoose,我在使用fast csv时遇到了一个小问题。问题是,当我将headers设置为true时,会得到“未定义”的数据(只有空数据会被推送到数据库) 然而,当我删除“stream,{headers:true}”时,它确实可以工作,但是头会被推送到数据库中 const db = require("../models"); const fs = require("fs"); const csv = require("fast-csv"); const csvfile = "./public/csv/ven

我在使用fast csv时遇到了一个小问题。问题是,当我将headers设置为true时,会得到“未定义”的数据(只有空数据会被推送到数据库)

然而,当我删除“stream,{headers:true}”时,它确实可以工作,但是头会被推送到数据库中

const db = require("../models");
const fs = require("fs");
const csv = require("fast-csv");
const csvfile = "./public/csv/vendor_example.csv";
const stream = fs.createReadStream(csvfile);

router.get("/import_data", (req, res, next) => {
  // csv.fromStream(stream, { headers: true });
  var csvStream = csv(stream, { headers: true })
    .on("data", function(data) {
      // products.push(data);
      var imported_data = new db.Products({
        // date: new Date(),
        column1: data[0]
      });

      imported_data.save(error => {
        console.log(skus_data);
        if (error) {
          throw error;
        }
      });
    })
    .on("end", function() {
      console.log("ended");
    });

  stream.pipe(csvStream);

  // res.json({ success: "Data Imported", status: 200 });
  res.json({
    success: "Data imported with no errors",
    status: 200
  });
});
有没有办法解决这个问题

谢谢大家!

找到了答案

  var imported_data = new db.Products({
    // date: new Date(),
    column1: data[0]
  });
我需要指定头的名称,而不是数据[0],例如:

第1列:data.name