我想在函数外检索变量“用户”:
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);
}
谢谢你的帮助 。