Background Information
I have a node / express application that connects to a Redis database to query a list of values. And then for each value in the return data, I need to do a further query.
Problem
The initial query (redis "scan" command) works fine, but the HGETALL query I attempt for each item in the initial result set is not behaving as expected.
To demonstrate this problem, I added a console.log command on line 34 to print out the value of the current scan record... and then I print it out again inside the callback function of the HGETALL (line 36) but the values are different. In my mind, they should be the same... but I'm sure it's something basic about the way node.js works, that I'm missing.
Code
27 router.get('/', function(req, res, next) {
28 redis.send_command("SCAN", [0,"MATCH", "emergency:*"], function(err, reply) {
29 if (reply) {
30 var retdata = [];
31 console.log('length is: ' + reply[1].length);
32 for (var i = 0; i < reply[1].length; i++) {
33 var emergIP = reply[1][i];
34 console.log('emergIP outside the call: ' + emergIP);
35 redis.hgetall(emergIP, function (err, data) {
36 console.log('emergIP inside the call: ' + emergIP);
37 if (data) {
38 var temp = emergIP.split(":");
39 var key = temp[1];
40 console.log('the key is: ' + key);
41 //retdata.push({key:data.callerid});
42 }
43 });
44 }//end for loop
45 res.send(JSON.stringify(retdata));
46 } //end if
47 else {
48 //no emergency data defined yet
49 var retval = {"res":false, "msg":"no emergency data defined"};
50 res.send(JSON.stringify(retval));
51
52 }
53 }); //end send_command
54 });
55
Output from the code
length is: 8
emergIP outside the call: emergency:10.1
emergIP outside the call: emergency:10.2
emergIP outside the call: emergency:10.13
emergIP outside the call: emergency:10.14
emergIP outside the call: emergency:10.18.90
emergIP outside the call: emergency:10.19
emergIP outside the call: emergency:10.20
emergIP outside the call: emergency:10.244
GET /emergency/ 200 220.368 ms - 2
emergIP inside the call: emergency:10.244
the key is: 10.244
emergIP inside the call: emergency:10.244
the key is: 10.244
emergIP inside the call: emergency:10.244
the key is: 10.244
emergIP inside the call: emergency:10.244
the key is: 10.244
emergIP inside the call: emergency:10.244
the key is: 10.244
emergIP inside the call: emergency:10.244
the key is: 10.244
emergIP inside the call: emergency:10.244
the key is: 10.244
emergIP inside the call: emergency:10.244
the key is: 10.244
Question
I think the problem is that I'm expecting the callbacks on the send_command function to happen sequentially. In other words, maybe the problem is that I'm not supposed to have a function call within another function call? This is my first node application... and I'm learning as I go.
Any suggestions would be appreciated.