9

I'm quite new to node and am trying to create something that gets some server info. But here's my problem. I setup a config object (this will, in time, become updated dynamically by events that occur) and then later in, in a function, I try and access a value in this object. (See code below)

So First, I setup my vars:

var util            = require('util'),
    child           = require('child_process'),
    config          = {};

which works okay. Then I load my config:

function loadConfig( )
{
    // Add some code for auto-loading of args
    config = {
        "daemons": [
            ["Apache", "apache2"],
            ["MySQL",  "mysqld"],
            ["SSH", "sshd"]
        ]
    };
}

and init that calling the function

loadConfig();

After that, I run my check on daemons.

function getDaemonStatus( )
{
    for(var i=0; i<config.daemons.length; i++)
    {

        child.exec( 'ps ax -o \'%c %P\' | awk \'{if (($2 == 1) && ($1 == "\'' +
            config.daemons[i][1] + '\'")) print $0}\'',
            function( error, stdout, stderr )
        {

            console.log(config.daemons[i]);
        });
    }
}

The response I get is:

undefined
undefined
undefined

I don't really want to use a GLOBAL variable, so can you guys think of another way to solve my problem?

Thanks! =]

Daniel Noel-Davies
  • 547
  • 1
  • 6
  • 10
  • possible duplicate of [Access outside variable in loop from Javascript closure](http://stackoverflow.com/questions/1331769/access-outside-variable-in-loop-from-javascript-closure) – Felix Kling Nov 06 '11 at 12:19

1 Answers1

13

This is a gotcha that lots of people run into because of the asynchronous ordering of execution.

Your for loop will look from 0-3, and then exit when 'i' is four, obviously. The tough part to remember here is that your callback for exec won't run immediately. In only runs once the process has started, and by the time that happens, the for loop will be done.

That means that essentially, all three times that your callback function is running, you are essentially doing this:

console.log(config.daemons[4]);

That's why it prints 'undefined'.

You need to capture the 'i' value in a new scope, by wrapping the loop contents in an anonymous, self-executing function.

function getDaemonStatus( ) {
    for(var i=0; i<config.daemons.length; i++) {
        (function(i) {

             child.exec( 'ps ax -o \'%c %P\' | awk \'{if (($2 == 1) && ($1 == "\'' +
                config.daemons[i][1] + '\'")) print $0}\'',
                function( error, stdout, stderr ) {

                console.log(config.daemons[i]);
            });

        })(i);
    }
}

Also, I see that your function is called 'getDaemonStatus'. Just remember that, since that exec callback is asyncronous, that also means that you can't collect the results of each callback, and then return them from the getDaemonStatus. Instead, you will need to pass a your own callback, and call the it from inside your exec callback.

Updated

Note though, the easiest way to have a scope per-iteration is to use forEach, e.g.

function getDaemonStatus( ) {
    config.daemons.forEach(function(daemon, i){
         child.exec( 'ps ax -o \'%c %P\' | awk \'{if (($2 == 1) && ($1 == "\'' +
            daemon[1] + '\'")) print $0}\'',
            function( error, stdout, stderr ) {

            console.log(daemon);
        });
    }
}
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • 1
    Thank you so much! I knew i had to do my own callback from within the exec callback, but now I understand the scopes.Really Appreciate your help! – Daniel Noel-Davies Nov 06 '11 at 14:25
  • This is personal, but I like to use a different name for that inner "i" variable to point out (to myself reading this a year later) that it is a different variable. So the inner function looks like - pls. excuse the lack of formatting - (function(inner) {...console.log(config.daemons[inner]);})(i); – Mörre Dec 12 '12 at 09:03