0

The following will read and import many CSV files from disk into MongoDB but NodeJS won't exit after importing all the files if it doesn't go through the resizePhoto() function (Which contains a process.exit after resizing the images).

How can I have it to close properly after importing all files without interrupting? If I add a process.exit .on end it will exit after importing the first file.

var importData = function(fileName) {

    // Get file from disk.
    var filePath = path.join(folder, fileName);

    // Read and import the CSV file.
    csv.fromPath(filePath, {
        objectMode: true,
        headers: keys
    })
    .on('data', function (data) {

        var Obj = new models[fileName](data);

        models[fileName].find({}).remove().exec();

        Obj.save(function (err, importedObj) {
            if (err) {
                console.log(fileName, err);
            } else if (fileName === 'PHOTOS') {
                resizePhoto(importedObj);
            }
        });

    })
    .on('end', function() {
        console.log(fileName + ': Imported.');
    });
};

module.exports = importData;
Gab
  • 2,047
  • 3
  • 30
  • 58

1 Answers1

0

Use the module async, method parallel (https://github.com/caolan/async#parallel). It can call your tasks (import) in parallel and call the final handler (exit) after all tasks end.

In your case:
1) Somewhere in project

csvImp=require('importData.js');
async.parallel([
    function(done){csvImp(name1,done);},
    function(done){csvImp(name2,done);},
    ...
    function(done){csvImp(nameN,done);}
],
function(err, results){
    process.exit();
});

2) in importData.js

var importData = function(fileName,done) {
...
.on('end', function() {
    console.log(fileName + ': Imported.');
    done();
});

So, next you need to prepare list of tasks. Something like

names.forEach(function(n){
    tasks.push(function(done){csvImp(n,done);});
});

And call async.parallel with tasks.

Good luck)

Mi Ke Bu
  • 214
  • 1
  • 12