javascript - Nodejs retrieve a variable -
i retrieve variable "users" outside function:
var users; client.lrange("comptes", 0, -1, function(err, replies) { replies.foreach(function(reply, i) { users = json.parse(reply.tostring()); }); }); // here retrieve "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 + ' not exist')); } } function findbyusername(username, fn) { (var = 0, len = users.length; < len; i++) { var user = users[i]; if (user.username === username) { return fn(null, user); } } return fn(null, null); } i use q or async module, not know how it..
thanks .
the variable in scope of use have, means can undefined if you've never given value (which don't if don't count callback). try using line var users = "damn not yet"; , that's you'll in console. means callback function lrange either isn't being called, or logic wrong.
Comments
Post a Comment