Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node script doesn't ever end

I have the node script below to basically copy the contents of some files and insert them to mongo.

The script never seems to end and even though all the data gets inserted successfully, I always have to do Ctrl+C to kill it.

Is there something i'm supposed to use in node.js to end a script?

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/testdb');
var dir = './seeds';
var db = mongoose.connection;

// Show connection error if there is one
db.on('error', console.error.bind(console, 'Database Connection Error:'));

// If we successfully connected to mongo
db.once('open', function callback() {

    var fs = require('fs'); // Used to get all the files in a directory

    // Read all the files in the folder
    fs.readdir(dir, function(err, list) {

        // Log the error if something went wrong
        if(err) {
            console.log('Error: '+err);
        }

        // For every file in the list
        list.forEach(function(file) {

            // Set the filename without the extension to the variable collection_name
            var collection_name = file.split(".")[0];
            var parsedJSON = require(dir + '/' + file);

            for(var i = 0; i < parsedJSON.length; i++) {

                // Counts the number of records in the collection
                db.collection('cohort').count(function(err, count) {
                    if(err) {
                        console.log(err);
                    }
                });

                db.collection(collection_name).insert(parsedJSON[i], function(err, records) {

                    if(err) {
                        console.log(err);
                    }

                    console.log(records[0]);
                    console.log("Record added as "+records[0]);

                });
            }

        });
    });
});
like image 441
Catfish Avatar asked Apr 22 '14 14:04

Catfish


People also ask

How do I end a node script?

Method 1: Using ctrl+C key: When running a program of NodeJS in the console, you can close it with ctrl+C directly from the console with changing the code shown below: Method 2: Using process. exit() Function: This function tells Node. js to end the process which is running at the same time with an exit code.

Does node JS run all the time?

NodeJS is a runtime environment on a V8 engine for executing JavaScript code with some additional functionality that allows the development of fast and scalable web applications but we can't run the Node. js application locally after closing the terminal or Application, to run the nodeJS application permanently.

How long will node js last?

js 16 (LTS) or Node. js 18 (soon to be LTS). Please, consider that Node. js 16 (LTS) will go End-of-Life in September 2023, which was brought forward from April 2024 to coincide with the end of support of OpenSSL 1.1.

Is NodeJS good in 2022?

Node. js development has become very popular over the last four years and continues to stand the competition in 2022 making startups worldwide choose it over other available options.


2 Answers

When everything is done, call mongoose.disconnect(). As @AaronDufour correctly points out, node will not exit while event handler callbacks are registered because it doesn't know that no more events are expected, like the connection emitting a 'close' or 'error' event, for example.

like image 170
Peter Lyons Avatar answered Oct 13 '22 20:10

Peter Lyons


you can call process.exit(); to exit

like image 9
m0etaz Avatar answered Oct 13 '22 21:10

m0etaz