Redis get function returns undefined

I am creating a very simple client-server application using node.js and redis for the first time. After successfully starting my redis client and my http server, I try to do a simple SET / GET with my redis client.

First I do:

client.set('apple', 10, redis.print);

which returns Reply:Ok.

Immediately after that I do:

client.get('apple', function(err, reply) {
    count = parseInt(reply);
    return count;
});

Strange, when countI print, I get undefined. But if I use redis.print, for example:

client.get('apple', redis.print);

A Reply: 10returns to the console. 10also displayed if i do console.log(count).

I thought that maybe I used the given functions in the module incorrectly, but, having addressed both of them, I am not sure what causes my errors:

node_redis - github readme

API redis.io - GET

+3
source
2

, undefined.

client.get('apple', function(err, reply) {
    count = parseInt(reply);
    return count;
});

console.log(count); // is this where you get undefined?
                    // count at this point is, in fact, undefined,
                    // because it set in a callback which was not yet fired.

node.js, . ? - ?

client.get('apple', function(err, reply) {
    count = parseInt(reply);
    // do something with the count here. Print it or whatever.
});

, - . - .

var count = null;
client.get('apple', function(err, reply) {
    count = parseInt(reply);
});

// TODO: wait for callback
console.log(count);
+2

, , .

, , :

client.get('apple', function (err, reply) {

console.log ();

});

0

All Articles