NodeJS + Redis HGETALL返回undefined

时间:2014-04-17 11:07:51

标签: node.js redis

在我看来,我需要一些帮助 我有NodeJS + Redis并尝试在nodejs的帮助下从Redis获取数据

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();

首先console.log返回正确的密钥对 即使我手动设置变量

,第二个console.log也总是返回undefined
...
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
....

对数据库的查询返回neessary对象:

    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"

但是如果我们删除外部forEach并手动设置密钥变量,代码就能正常工作:

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();

并返回

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

我还尝试添加回调

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();

没有结果

1 个答案:

答案 0 :(得分:0)

我添加了错误抛出,正如Bnif建议的那样 并看到了错误

[Error: Redis connection gone from close event.]

这是一个愚蠢的错误,代码在异步模式下工作,因此在第二个查询开始工作之前连接已关闭。

这是结果正确的代码

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