-2

I'm trying to execute an hdel command in node.js inside a hget block. Here's the code:

client.hget(requests[i], "client", function(err, client){
if(isUser == true){
    client.hdel(requests[i], function(err){
          if(err){
                 console.log("cannot process request");
              }
     });
    }
});

It's not working and I can't understand why! Any help would be greatly appreciated.

C.O.D.E
  • 885
  • 8
  • 19

1 Answers1

0

Since you use requests[i] as a parameter, we can assume this block of code is encapsulated in a loop: perhaps you are trying to iterate on an array and executing hget/hdel for each item.

In that case, there is a good chance you have been hit by the scoping rules of Javascript: requests[i] is part of a closure, but a closure can only be defined at the function level (not at block level).

You probably need to define an inner function, or to use forEach to iterate on your container. More information here:

nodejs, redis. check if keys exists and create new if not

For loop get items from redis delay

Community
  • 1
  • 1
Didier Spezia
  • 70,911
  • 12
  • 189
  • 154