node_redis client - Cannot return out values from a query

I'm going crazy with a problem which I can not find solution. I can not return out values from a redis query. I am using node_redis client (http://redis.js.org/) as redis driver for Node.JS.

The problem is: I wrote a function for get the user status connection and return it for take the value from other function.

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

But function return nothing!. If I replace the return line with a console.log(), it work! but I don't need a console.log(), I need take the value.

I tried too create a variable outside of the query and set it from inside and then return it but, it doesn't work.

Somebody know how may I do that?

Thank you!

Answers:

Answer

I'm not a redis expert but ... maybe you simply missing the return keyword?

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

Based on manual from http://redis.io/commands/MULTI multi returns value, which you doesn't return.

Answer

In this site but, in spanish I received the right answer. I post here for help to others:

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

And then I catch the returned value as you will see below:

// 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!");

Thank you to everyone! I hope I've helped!

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.