1
votes

seemed to me, I need some help I have NodeJS + Redis and trying to get data from Redis with help of nodejs

var redis = require('redis'),client = redis.createClient();
client.on("error", function (err) {
  console.log("Error: " + err);
});
client.keys("currency:*", function (err, keys) {
        keys.forEach(function (key, i) {
            console.log(key);
            client.hgetall(key, function (err, currencyData) {
                console.log(currencyData);
            })
        });
});
client.quit();

first console.log returns the correct key pair the second console.log always return undefined even if I manually set the variable

...
currency:13977251354940.4693533380050212
currency:13977251347600.9115635070484132
currency:13977251346660.3178929006680846
currency:13977251355930.11270855111069977
currency:13977251349960.9243609569966793
currency:13977251353130.7620244727004319
currency:13977251345050.8327386728487909
currency:13977251355830.1213580728508532
currency:13977251351530.48609258118085563
currency:13977251349030.10637291427701712
currency:13977251348590.6786048328503966
...
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
....

the query to the database returns the neessary object:

    127.0.0.1:6379> hgetall "currency:13977251347740.9971425810363144"
 1) "date"
 2) "24/01/14"
 3) "time"
 4) "21:03:00"
 5) "high"
 6) "1.3678000"
 7) "low"
 8) "1.3673000"
 9) "close"
10) "1.3677000"
11) "open"
12) "1.3676000"

But if we remove the external forEach and manually set the key variable, the code correctly works:

var redis = require('redis'),client = redis.createClient();
client.on("error", function (err) {
  console.log("Error: " + err);
});
client.hgetall("currency:13977251348590.6786048328503966", function (err, currencyData) {
   console.dir(currencyData);
})
client.quit();

and returns

{ date: '30/01/14',
  time: '22:26:00',
  high: '1.3554000',
  low: '1.3552000',
  close: '1.3552000',
  open: '1.3553000' }

I've also tried to add callback

var redis = require('redis'),client = redis.createClient();
client.on("error", function (err) {
  console.log("Error: " + err);
});
var myKeys = new Array();
function getKeys(callback) {  
    client.keys("currency:*", function (err, keys) {
            keys.forEach(function (key, i) {
                myKeys.push(key);
            });
        callback();
    });      
}  
getKeys(function() {  
    myKeys.forEach(function(key){
        client.hgetall(key, function (err, currencyData) {
            if(!(currencyData===undefined))
                    console.dir(currencyData);
        })
    }) 
});  
client.quit();

with no result

1
did you try to see if the variable err is defined and if it outputs something? if you are not getting any error, i think it could be an async problem. The outer request is finishing before the inner request. you could try to get all the Keys first and then querying the second part. - Bruno Faria
I tried but the err throw was already added here client.on("error", function (err) { console.log("Error: " + err); }); I also added the callback code to the issue - DeFate

1 Answers

1
votes

I added the error throw, as Bnif advised and saw the Error

[Error: Redis connection gone from close event.]

It was a silly mistake, the code worked in an asynchronous mode, so the connection was closed before the second query begins working.

Here is the result correct code

var redis = require('redis'),client = redis.createClient();
client.on("error", function (err) {
  console.log("Error: " + err);
});
var myKeys = new Array();
function getKeys(callback) {  
    client.keys("currency:*", function (err, keys) {
            keys.forEach(function (key, i) {
                myKeys.push(key);
            });
        callback();
    });      
}  
getKeys(function() {  
    myKeys.forEach(function(key){
        client.hgetall(key, function (err, currencyData) {
            if (err) 
                    console.log(err);
            console.dir(currencyData);
        })
    }) 
    client.quit();
});