开发者

function is not returning data

I have the following code

    var redis = require("redis"),
    client 开发者_StackOverflow= redis.createClient();

var getuser = function(username) {
  var userhash={};
  client.hgetall("users."+username, function(err, user) {
    userhash=user;
  });
  return userhash;
};
user_rahul = { 
     username: 'rahul',
     queueno: 1,
     sessionId: '6604353811126202'

   };
user_namita = {
  username: 'namita',
  sessionId:'2'
};
client.hmset('users.rahul', user_rahul);
client.hmset('users.namita', user_namita);
var username = "rahul"; // From a POST perhaps
  var user1=getuser(username);
  console.log(user1);
client.hgetall("users." + username, function(err, user) {
  console.log(user);

});

i have created a getuser function to return the value of particular username in userhash but it is not returning please help in finding why it is not returning . what should i do to get the value return ?


You're mixing sync and async patterns when you make an async call in your sync getUser function. You need to make your getUser function async - e.g:

var redis = require("redis"),
    client = redis.createClient();

var getuser = function(username, cb) {
  client.hgetall("users."+username, cb);
};

user_rahul = { 
     username: 'rahul',
     queueno: 1,
     sessionId: '6604353811126202'

   };
user_namita = {
  username: 'namita',
  sessionId:'2'
};

client.hmset('users.rahul', user_rahul);
client.hmset('users.namita', user_namita);
var username = "rahul"; // From a POST perhaps
getuser(username, function(err, res){
     console.log(res);
 });
client.hgetall("users." + username, function(err, user) {
  console.log(user);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜