插入到mongodb中的node.js

时间:2018-07-01 02:18:19

标签: node.js mongodb

我正在尝试编写一个简单的程序,将犯罪列表存储到mongodb数据库中,但是在过去的几个小时中,我尝试了许多解决方案,并且看来Node.js只看到一个数据,并且它是未定义的!是的,文件interactionscitoyendo.csv不是空的,位于javascript文件的旁边。有什么想法吗?

var fs = require('fs');
var file = fs.createWriteStream('interventionscitoyendo.csv');
var http = require('http');
var iclite = require("iconv-lite");
var csv = require('csv');
var object = csv();
var db = null;
var request = http.get("http://donnees.ville.montreal.qc.ca/dataset/5829b5b0-ea6f-476f-be94-bc2b8797769a/resource/c6f482bf-bf0f-4960-8b2f-9982c211addd/download/interventionscitoyendo.csv", function(response) {
response.pipe(file);
});

//Create Data-Base
var mongo = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017/";

mongo.connect(url,{ useNewUrlParser: true }, function(err, database) {
if (err){ console.log(err);}
db = database;
var dbo = db.db("Crimes");
var ListeCrimes = [];
object.from.path('interventionscitoyendo.csv').to.array(function (data) {
for (var index = 0; index < data.length; index++) {
  var crime = {
    CATEGORIE: iclite.decode(data[index][0], "ISO-8859-1"),
    DATE: data[index][1],
    QUART: data[index][2],
    PDQ: data[index][3],
    X: data[index][4],
    Y: data[index][5],
    LATITUDE: data[index][6],
    LONGITUDE: data[index][7]
  };
  console.log(crime);
  ListeCrimes.push(crime);
}
console.log(data.length);
dbo.collection("Crimes").insertMany(ListeCrimes, function(err, res) {
    if (err){ console.log(err);}
    else { console.log("A1 works"); }
});
if (data.status == 200){
  db.close();
}
});
});

输出为:

{ CATEGORIE: '',
DATE: undefined,
QUART: undefined,
PDQ: undefined,
X: undefined,
Y: undefined,
LATITUDE: undefined,
LONGITUDE: undefined }
1
A1 works

1 个答案:

答案 0 :(得分:2)

“文件”没有完全写入,由于没有数据,因此开始读取。您应该等待 finish 事件引发。

file.on('finish', function() {
});

在此 finish 事件处理程序中,使用async方法关闭文件:file.close。
关闭文件后,您可以读取并插入mongo db中。

file.on('finish', function() {
    file.close(function() {
        // Insert in mongoDB here
    });
});

等待文件完成管道传输后,解决方案应为:

var fs = require('fs');
var file = fs.createWriteStream('interventionscitoyendo.csv');
var http = require('http');
var iclite = require("iconv-lite");
var csv = require('csv');
var object = csv();
var db = null;
var request = http.get("http://donnees.ville.montreal.qc.ca/dataset/5829b5b0-ea6f-476f-be94-bc2b8797769a/resource/c6f482bf-bf0f-4960-8b2f-9982c211addd/download/interventionscitoyendo.csv", function(response) {
    response.pipe(file);
});

file.on('finish', function() {
    file.close(function() {
      // Create Data-Base
      var mongo = require("mongodb").MongoClient;
      var url = "mongodb://localhost:27017/";

      mongo.connect(url,{ useNewUrlParser: true }, function(err, database) {
          if (err){ console.log(err);}
          db = database;
          var dbo = db.db("Crimes");
          var ListeCrimes = [];
          object.from.path('./interventionscitoyendo.csv').to.array(function (data) {
              for (var index = 0; index < data.length; index++) {
                  var crime = {
                      CATEGORIE: iclite.decode(data[index][0], "ISO-8859-1"),
                      DATE: data[index][1],
                      QUART: data[index][2],
                      PDQ: data[index][3],
                      X: data[index][4],
                      Y: data[index][5],
                      LATITUDE: data[index][6],
                      LONGITUDE: data[index][7]
                  };
                  console.log(crime);
                  ListeCrimes.push(crime);
              }
              console.log(data.length);
              dbo.collection("Crimes").insertMany(ListeCrimes, function(err, res) {
                  if (err){ console.log(err);}
                  else { console.log("A1 works"); }
              });
              if (data.status == 200){
                  db.close();
              }
          });
      });
    });
});