简体   繁体   中英

node.js to insert into mongodb

I'm trying to write a simple program that stores a list of crimes into a mongodb database, however I've tried many solutions for the last few hours and it seems like Node.js only sees one data and it's undefined! Yes the file interventionscitoyendo.csv is not empty and is located right next to the javascript file. Any ideas?

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();
}
});
});

And the output is :

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

'file' is not completely written, and it starts being read because there is no data. You should wait for finish event to raise.

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

In this finish event handler, close the file using the async method: file.close.
When the file is closed, you can read and insert in mongo db.

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

After waiting for files to finish piping, the solution should be the following:

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();
              }
          });
      });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM