函数不返回数据

发布于 2024-11-11 17:29:54 字数 778 浏览 0 评论 0原文

我有以下代码,

    var redis = require("redis"),
    client = 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);

});

我创建了一个 getuser 函数来返回 userhash 中特定用户名的值,但它没有返回,请帮助查找它没有返回的原因。 我应该怎么做才能获得价值回报?

I have the following code

    var redis = require("redis"),
    client = 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2024-11-18 17:29:54

当您在同步 getUser 函数中进行异步调用时,您正在混合同步和异步模式。您需要使 getUser 函数异步 - 例如:

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);
});

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);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文