gpt4 book ai didi

node.js - NodeJS+Redis HGETALL 返回undefined

转载 作者:可可西里 更新时间:2023-11-01 11:00:52 44 4
gpt4 key购买 nike

在我看来,我需要一些帮助我有 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返回正确的 key 对即使我手动设置变量,第二个 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
....

对数据库的查询返回必要的对象:

    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 并手动设置 key 变量,代码就可以正常工作:

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

没有结果

最佳答案

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

关于node.js - NodeJS+Redis HGETALL 返回undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23131500/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com