0

I have a simple codesnippet in a file test.js:

  [1, 2, 3, 4].forEach(function(e) {
        console.log(e);
    });

whenever I run node test.js in terminal I get the output

    1
    2
    3
    4
    |

But the program never really exits. I am required to end it manually. It seems quite trivial but I am unable to figure out a proper way to exit the scrip in the terminal.

UPDATE 1:

var mongoose = require('mongoose');

var User = require('../models/users/user');
var UserProfile = require('../models/users/profile');

var config = require('../config');
var logger = require('../libraries/logger');
logger = logger.createLogger(config);

var connectionString = config.database.adapter + '://' + config.database.host + ':' + config.database.port + '/' + config.database.name;
mongoose.connect(connectionString, {server: {auto_reconnect: true }});

User.find(function(error, users) {
    users.forEach(function(user) {
    var data = {
        email: user.local.email || user.google.email || user.facebook.email || ''
    };
    UserProfile.update({user_id: user._id}, {$set: data}, function (error, record) {
        if (error) {
            logger.error(error);
        } else {
            logger.info(data);
        }
    });
});

Above is the code I am actually trying to make work. Adding process.exit() exits the process even without processing the script. Any solutions?

UPDATE 2:

I figured out the solution. In above case script wasn't exiting because connection to mongodb database wasn't closed.

I used a dirty hack to close the connection after processing is done by adding setTimeout(function() { mongoose.connection.close(); }, 60 * 1000); at the end of the line

9
  • And this doesn't happen without the forEach loop? Commented Nov 26, 2015 at 16:53
  • it won't happen when it is a blocking script. But for non-blocking it seems to happen. I found how to make it work. But I don't understand why it is happening without using process.exit() Commented Nov 27, 2015 at 7:13
  • There is nothing non-blocking in the script you've posted? Please show us your actual code. Yes, non-blocking code can lead to node not terminating when written oddly. Commented Nov 27, 2015 at 7:21
  • I am under the impression that whenever we are using callbacks it is a non-blocking code. In the current example this is the case isn't it? Commented Nov 27, 2015 at 7:26
  • No Commented Nov 27, 2015 at 7:31

1 Answer 1

1

If this is a nodejs question which it appears to be then you exit using process.exit() so you add that to the end of whatever function should run last. In your case you would close mongoose first and hen have the process.exit() as the success handler for the exiting of mongoose

Sign up to request clarification or add additional context in comments.

3 Comments

yes it is a nodejs script. I added process.exit() in the end of the file and it works. I wonder why it happened. Would you be able to explain what happened at the run time?
Nodejs often times runs processes in the background so if your script doesn't have a specific end point it just keeps running
And if it is the right answer please mark it as such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.