Possible Duplicate:
How to pass values to a callback function at the time of the request call?
I know this is a general problem and not only in node.js. I have to pass a variable from the for loop to a function which I want to generate. The functions are stored in an array and have to remember these variables.
Here is my example:
for(var i = 0; i < administratorIds.length; i ++){
adminCallbacks[adminCallbacks.length] = function(callback){
model_user.getById(administratorIds[i], function(user){
administrators[administrators.length] = {name: user.getName()};
callback(null, null);
});
};
}
You need to create a new variable scope for each iteration of the loop.
Only way to do that will be by invoking a function.