NodeJS Dynamic Eventing

So here’s something…

I was trying to define things so I can key/value query later in nodeJS. I have an array of usernames. Then I try and create custom end points using nodeJS and express to customize a json return. If you pull down the code and run it you’ll see what I mean:


var FOLLOWS = new Array('GSPBetaGroup', 'FreedomRiders1');

var lastTweets = new Array();
lastTweets["FreedomRiders1"] = "this is a piece of content!";

for(var t = 0; t < FOLLOWS.length; t++) {
    
    app.get('/'+FOLLOWS[t]+'.json', function(request, response) {  
        
        console.log('loggin the function get last tweets')
        console.log(getLastTweets(x))
        console.log('nrt')

        console.log('loggin lastTweets')
        console.log(lastTweets)
        console.log('nrt')

        console.log('testing how to call out an associative arrays')
        console.log('lastTweets.FreedomRiders1 = ' + lastTweets.FreedomRiders1);
        console.log('lastTweets["FreedomRiders1"] = ' + lastTweets["FreedomRiders1"]);
        console.log('lastTweets['FreedomRiders1'] = ' + lastTweets['FreedomRiders1']);
        console.log('nrt')

        console.log('FOLLOWS = ' + FOLLOWS);
        console.log('x = ' + x);
        console.log('FOLLOWS.length = ' + FOLLOWS.length);
        console.log('FOLLOWS[t] = ' + FOLLOWS[x]);
        console.log('FOLLOWS["t"] = ' + FOLLOWS["t"]);
        console.log('FOLLOWS.t = ' + FOLLOWS.t);
        console.log('nrt')
        
        console.log('lastTweets[FOLLOWS[t]] = ' + lastTweets[FOLLOWS[t]]);
        console.log('nrt')
        
        for(var x = 0; x < FOLLOWS.length; x++) {
            console.log('loggin the function get last tweets')
            console.log(getLastTweets(x))
            console.log('nrt')

            console.log('loggin lastTweets')
            console.log(lastTweets)
            console.log('nrt')

            console.log('testing how to call out an associative arrays')
            console.log('lastTweets.FreedomRiders1 = ' + lastTweets.FreedomRiders1);
            console.log('lastTweets["FreedomRiders1"] = ' + lastTweets["FreedomRiders1"]);
            console.log('lastTweets['FreedomRiders1'] = ' + lastTweets['FreedomRiders1']);
            console.log('nrt')
            
            console.log('FOLLOWS = ' + FOLLOWS);
            console.log('x = ' + x);
            console.log('FOLLOWS.length = ' + FOLLOWS.length);
            console.log('FOLLOWS[x] = ' + FOLLOWS[x]);
            console.log('FOLLOWS["x"] = ' + FOLLOWS["x"]);
            console.log('FOLLOWS.x = ' + FOLLOWS.x);
            console.log('nrt')
            
            console.log('t = ' + t)
            console.log('x = ' + x)
            console.log('FOLLOWS = ' + FOLLOWS)
            console.log('FOLLOWS[t] = ' + FOLLOWS[t])
            console.log('FOLLOWS[x] = ' + FOLLOWS[x])
            console.log('nrt')
            
            console.log('lastTweets[FOLLOWS[x]] = ' + lastTweets[FOLLOWS[x]]);
            console.log('nrt')
        }
        
            
//        response.json({anObject: lastTweet});
//        lastTweets[name] = null;
    });
}

You can see that everything inside the second for loop is defined whereas everything outside is undefined. Why is this? I know it has something to do with t ending up being equal to the length of the array, but I don't understand why it would not loop through t = 0, t= 1 first?


			

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.