【问题标题】:node_redis client - Cannot return out values from a querynode_redis 客户端 - 无法从查询中返回值
【发布时间】:2016-08-20 06:48:51
【问题描述】:

我正在为一个我找不到解决方案的问题而发疯。我无法从 redis 查询中返回值。我正在使用 node_redis 客户端 (http://redis.js.org/) 作为 Node.JS 的 redis 驱动程序。

问题是:我写了一个函数来获取用户状态连接并返回它以从其他函数中获取值。

//CHECK USER STATUS
exports.checkUserStatus = function(uid){
    redis.multi()
        .sismember('users:online',uid)
        .sismember('users:iddle', uid)
        .sismember('users:out', uid)
        .sismember('users:locked', uid)
        .exec(function(err, res){
            res.forEach(function(res, index){
                if(res != 0){
                    switch(index){
                        case 0:
                            return 'online';
                            break;
                        case 1:
                            return 'iddle';
                            break;
                        case 2:
                            return 'out';
                            break;
                        case 3:
                            return 'locked';
                            break;
                        default:
                            return 'offline';
                    }
                }
            });
        })
}

但是函数什么也不返回!如果我用 console.log() 替换返回行,它就可以工作!但我不需要console.log(),我需要取值。

我也尝试在查询之外创建一个变量并从内部设置它然后返回它,但是它不起作用。

有人知道我该怎么做吗?

谢谢!

【问题讨论】:

    标签: javascript node.js return-value node-redis


    【解决方案1】:

    我不是 redis 专家,但是……也许您只是缺少 return 关键字?

    var result;
    exports.checkUserStatus = function(uid){
      redis.multi()
      ...
        .exec(function(err, res){
            res.forEach(function(res, index){
                if(res != 0){
                    switch(index){
                        case 0:
                            result = 'online';
                            break;
                        case 1:
                            result ='iddle';
                            break;
                        case 2:
                            result = 'out';
                            break;
                        case 3:
                            result = 'locked';
                            break;
                        default:
                            result = 'offline';
                    }
                }
            });
        })
        return result;
    }
    

    基于来自http://redis.io/commands/MULTI multi 的手册返回值,您不会返回。

    【讨论】:

    • 是的!工作!我不知道我是不是太累了,还是什么,我尝试了你的代码以确保它可以工作,但我可以发誓我已经尝试过了......非常感谢!
    【解决方案2】:

    在这个网站上,但在西班牙语中,我得到了正确的答案。我在这里发帖寻求帮助:

    //CHECK USER STATUS ... Take a look at the new parameter 'callback' in the function!
    
    exports.checkUserStatus = function(uid, callback){
        redis.multi()
            .sismember('users:online',uid)
            .sismember('users:iddle', uid)
            .sismember('users:out', uid)
            .sismember('users:locked', uid)
            .exec(function(err, res){
                res.forEach(function(res, index){
                    if(res != 0){
                        switch(index){
                            case 0:
                                // Here invoke the user's function
                                // and I pass the info
                                callback('online'); 
                                break;
                            case 1:
                                callback('online'); 
                                break;
                            case 2:
                                callback('online'); 
                                break;
                            case 3:
                                callback('locked'); 
                                break;
                            default:
                                callback('offline'); 
                        }
                    }
                });
            })
    }
    

    然后我捕获返回的值,如下所示:

    // sequence of events
    // This happend first - #1
    checkUserStatus('uid-12343', function(status, user) {
        // This happend at the end - #2
        // Here we catch the status in asynchronous way
    });
    
    // This happend second - #2
    console.log("This happend before the redis info arrive!");
    

    谢谢大家!希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多