Not getting correct result inside Javascript loop

I am using Node.js with Octonode (GIThub plugin) to get latest tags for some plugins I use. I got the bulk of it working but the loop is throwing me for a loop.

Inside the client.get() the i var is coming back as 2 and just labeling plugin. to the last item in the JSON object.

It's probably something stupid, but I just can't figure out what.

Any help appreciated.

Here is what I have so far.

var github = require('octonode');
var client = github.client();


var mydata = {plugins:[
            {user:'RobinHerbots',repo:'jquery.inputmask',name:'jQuery-InputMask',myversion:'3.1.26'},
            {user:'getify',repo:'LABjs',name:'LAB.js',myversion:'2.0.3'}
         ]};

for (var i = 0; i < mydata.plugins.length; i++) {
 var plugin = mydata.plugins[i];
    client.get("/repos/"+plugin.user+"/"+plugin.repo+"/tags", {}, function(err, status, body, headers){
    console.log(i);
        if(status === 200) {
            var ghresult = body[0]; 
            //console.log(plugin.name+" current version: "+ ghresult.name +" :: My Version: "+ plugin.myversion);
            console.log(ghresult.name,i);
        } else {
            console.log(err);
        }
    });     
}

I don't have time to test this, but my first thought is you may need a closure to maintain the i variable in the callback context:

var github = require('octonode');
var client = github.client();


var mydata = {plugins:[
            {user:'RobinHerbots',repo:'jquery.inputmask',name:'jQuery-InputMask',myversion:'3.1.26'},
            {user:'getify',repo:'LABjs',name:'LAB.js',myversion:'2.0.3'}
         ]};

for (var i = 0; i < mydata.plugins.length; i++) {
 var plugin = mydata.plugins[i];
 ( function(i) {
    client.get("/repos/"+plugin.user+"/"+plugin.repo+"/tags", {}, function(err, status, body, headers){
        console.log(i);
        if(status === 200) {
            var ghresult = body[0]; 
            //console.log(plugin.name+" current version: "+ ghresult.name +" :: My Version: "+ plugin.myversion);
            console.log(ghresult.name,i);
        } else {
            console.log(err);
        }
    });
 })( i );
}

EDIT

I went back and tested this on my node install for you:

node test.js
1
2.0 1
0
3.1.26 0

Seems to be working fine for me. You may want to make sure you copied the code correctly. This was tested on node.js version 0.10.31.