Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

node.js - hdel inside hget block nodejs redis

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...