Question

for (i=0 ; i < pri.state.totalConversations; i++) {
            redisclient.lindex("conversationsIDList",i, function(err,reply) { 
            convID = reply;
            console.log("ConvID: " + "i: " + i + " "+  convID);         
});
          if(convID == pri.state.lastUpdatedConversationID)
            break;
            redisclient.hget("conversations", convID, function(err,reply) {
            console.log("ConvID hget: "+ convID + " "+ reply);
                data = JSON.parse(reply);
                console.log("data: " +data);

            });

above is a snippet of my code. redis-cli hget command returns the right answer which confirms that i have the hash setup correctly with the right key and field/values. However, the code snippet above returns "null" for hget, (convID has correct value which is the field for hget) - i dont understand why -

Était-ce utile?

La solution

convID isn't going to be set until your redisclient.lindex callback returns. That isn't going to happen until your for loop has long been exited (since node's event loop won't even run until you've gotten out of your loop). You're firing off a whole bunch of asynchronous requests to redisclient and not waiting for any of them to come back.

It's somewhat hard for me to tell exactly what you want to do, but you might want to look into flow-of-control libraries like Async.

It might be possible to define a boolean flag variable (scoped to your module or whatever function initiated your loop) that would be set by your first successful callback and would tell subsequent callbacks to ignore their results. But I'm just guessing. The main problem is that you're trying to use synchronous flow-of-control in an asynchronous environment.

Autres conseils

The problem is not with Redis, but rather with your understanding of node.js asynchronous programming. The Redis client library is asynchronous (like most node.js libraries). You cannot use it as if it was synchronous.

The hget command is sent before Redis has a chance to send the result of the lindex command. The convID variable is not set to what you expect, at the time you expect. The for loop exits before any result can be processed.

You should put all the code depending on convID in the callback of lindex, and you probably need an extra callback in the end to be called when all the results have been processed.

See an example at For loop get items from redis delay

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top