How do you access parent scope from a callback. The callback is function (err, obj). The var to_user_id is the same in all iterations. It looks like the callbacks are processed after all the iterations are done so the var to_user_id is only one value for all callbacks.
for(var i = 0, len = keys.length; i < len; i++) {
to_user_id = keys[i].replace('m', '')
client.get(keys[i], function (err, obj) {
//var not updating, why is both to_user_id=77
console.log("match: to_user_id=" + to_user_id + " from_user_id=" + obj)
var match = "match: to_user_id=" + to_user_id + " from_user_id=" + obj
io.emit(1, match);
});
}
Output
See how to_user_id is 77 for both iterations. One should be 6 and the last should be 77.
match: to_user_id=77 from_user_id=77
match: to_user_id=77 from_user_id=6
client.get is a redis function just in case you're wondering.