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

surendrapanday
  • 349
  • 1
  • 11
pntripathi9417
  • 248
  • 1
  • 11
  • And this doesn't happen without the `forEach` loop? – Bergi Nov 26 '15 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()` – pntripathi9417 Nov 27 '15 at 07: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. – Bergi Nov 27 '15 at 07: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? – pntripathi9417 Nov 27 '15 at 07:26
  • [No](http://stackoverflow.com/q/21884258/1048572) – Bergi Nov 27 '15 at 07:31
  • @Bergi Thanks for the reference. I got the better the understanding about how JS works. – pntripathi9417 Nov 27 '15 at 07:40
  • @Bergi I have added the complete code in the question. Adding `process.exit()` to this doesn't seem to work at all. – pntripathi9417 Nov 27 '15 at 07:56
  • You're update is an entirely separate question and should have been treated as such. However since you already did it to compensate for your new question – Binvention Nov 28 '15 at 04:35
  • @Binvention Yes the question becomes entirely different. I apologise for that. I misunderstood the problem I was having. Thanks for the help. – pntripathi9417 Nov 28 '15 at 06:32

1 Answers1

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

Binvention
  • 1,047
  • 1
  • 7
  • 23
  • 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? – pntripathi9417 Nov 27 '15 at 07:09
  • Nodejs often times runs processes in the background so if your script doesn't have a specific end point it just keeps running – Binvention Nov 27 '15 at 07:10
  • And if it is the right answer please mark it as such. – Binvention Nov 27 '15 at 07:11