node_redis - I can not return a value from inside a query

0

I'm going crazy with a problem that I can not find a solution for. I can not return values from inside a query to Redis. I'm using the Redis client for NodeJS node_redis ( link ).

The problem is this: I wrote a function to get the connection status of a user and return it to be used in another 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, the function does not return anything! If I replace the "return" line with a "console.log ()", it works! but, I do not need a console output, I need to capture the value in a variable, in an external function!

I also tried to create a variable within the function but, outside the query, fill it in the query and then export it from outside the query but, it does not work.

Another thing I tried to do was add the statement "return" at the beginning of the query (something that appears in the driver's documentation), but, it always returns TRUE, which is not the value of the query but it ended up run.

Does anyone know how to do this?

Greetings and thanks!

    
asked by Dogui 19.08.2016 в 23:43
source

1 answer

0

I do not know the library, but this happens as javascript works (asynchronously for I / O). The point is that this return is not the return of the function you are exporting but the internal callback (the second level).

The traditional way to do this in javascript is to continue with the callbacks or solve it using Promises (topic that I will not address, I recommend you investigate it as it helps to deal with the callback hell ).

This works like this: when the redis API obtains the answer, a function of the user is invoked where the data is finished processing.

The modified function: (with comments)

//CHECK USER STATUS ... fijate que agregue un parametro, la funcion del usuario
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:
                            // aqui invocas la funcion del usuario
                            // y le pasas todos los datos que puedan ser utiles
                            callback('online', res, index); 
                            break;
                        case 1:
                            callback('iddle', res, index); 
                            break;
                        case 2:
                            callback('out', res, index); 
                            break;
                        case 3:
                            callback('locked', res, index); 
                            break;
                        default:
                            callback('offline', res, index); 
                    }
                }
            });
        })
}

Then you use it this way:

// de paso comentare la secuencia de eventos
// esto sucede #1
checkUserStatus('uid-12343', function(status, user) {
    // esto sucede #3, en el futuro proximo... 
    // aqui tienes el resultado ... de forma asincronica.
});

// esto sucede #2
console.log("esto pasa antes de que llegue la respuesta de REDIS");

Keep in mind that this method is very simplified so that you understand the problem and how to approach the solution because in NodeJs you have to use it constantly and it is fundamental to understand it.

Care! Keep in mind that callback could be invoked more than once since all this is within a forEach , which I do not know what is the reason or its function but it does not matter so you understand how the thing works.

Of all this, you can take in clean, that there is no way that an asynchronic function can return a response in a synchronic way. Either you use callbacks, or you use promises.

Greetings.

    
answered by 20.08.2016 / 00:04
source