Nodejs retrieve a variable

I would like to retrieve the variable "users" outside the function:

    var users;

    client.lrange("comptes", 0, -1, function(err, replies) {

    replies.forEach(function(reply, i) {
      users = JSON.parse(reply.toString());
    });
});

// here to retrieve the "users" variable
console.log(users); // = undefined

function findById(id, fn) {
  var idx = id - 1;
  if (users[idx]) {
    fn(null, users[idx]);
  } else {
    fn(new Error('User ' + id + ' does not exist'));
  }
}

function findByUsername(username, fn) {
  for (var i = 0, len = users.length; i < len; i++) {
    var user = users[i];
    if (user.username === username) {
      return fn(null, user);
    }
  }
  return fn(null, null);
}

I use Q or async module, but I do not know how to do it..

thanks for your help .

The variable is in the scope of the use you have, which means that it can only be undefined if you've never given it a value (which you don't if you don't count the callback). Try using the line var users = "damn not yet"; and that's probably what you'll get in the console. That means that your callback function for lrange either isn't being called, or the logic is wrong.

something like this ?

getcomptes = function(callback){
    client.lrange('comptes', 0, -1, function(err, users){
            callback(err, users);
    });
};