我使用redisDB保存一些数据。所以我尝试使用Nodejs从我的Redis中获取一些数据。
首先,数据正常到达。但是问题出在“ hget()”中。 在函数hget()中,我可以正常处理数据,但是当我尝试在函数外部保存值时,它不起作用。
如果你们看到我的代码,您就可以清楚地理解
尝试很多事情..但是猫可以解决这个问题。.
class CenterNode {
constructor() {
this.nodeID = "centerNode1";
this.nodeIP = "some ip..";
this.edgeList = [];
}
}
class EdgeNode {
constructor(id, ip) {
this.id = id;
this.ip = ip;
}
}
function makeJSONobject(redisClient, centerNode){
var edgeNode = new EdgeNode();
var returnNode = new CenterNode();
redisClient.keys('*', function(err, keys){
console.log("Key number: ", keys.length);
for(let key of keys){
redisClient.hget(key, "IP", function(err, value){
edgeNode.id = key;
edgeNode.ip = value;
centerNode.edgeList.push(edgeNode);
// Point A
console.log("In hget: ", centerNode);
});
}
});
// This part is problem!!!
// Point B
console.log("centerNode: ", centerNode);
}
client = redis.createClient(6379, "My-Redis-Address");
client.auth("My-Redis-passwd");
client.on("error", function (err) {
console.log("Error " + err);
});
makeJSONobject(client, centerNode);
这是我的控制台输出
centerNode: CenterNode { nodeID: 'centerNode1', nodeIP: 'some-ip', edgeList: [] }
Key number: 2
In hget: CenterNode {
nodeID: 'centerNode1',
nodeIP: 'some-ip',
edgeList: [ EdgeNode { id: '201905020001', ip: 'some-ip' } ] }
In hget: CenterNode {
nodeID: 'centerNode1',
nodeIP: 'some-ip',
edgeList:
[ EdgeNode { id: '201904230001', ip: 'some-ip' },
EdgeNode { id: '201904230001', ip: 'some-ip' } ] }}
但是我想像这样使'centerNode'...要正常获取endgeList。.::
centerNode: CenterNode {
nodeID: 'centerNode1',
nodeIP: 'some-ip',
edgeList:
[ EdgeNode { id: '201904230001', ip: 'some-ip' },
EdgeNode { id: '201904230001', ip: 'some-ip' } ] }}
Plz,帮助。...