hget 块内的 hdel nodejs redis [英] hdel inside hget block nodejs redis
问题描述
我正在尝试在 node.js 中的 hget 块中执行 hdel 命令.代码如下:
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.
推荐答案
既然你使用 requests[i] 作为参数,我们可以假设这块代码被封装在一个循环中:也许你正试图迭代一个数组并为每个项目执行 hget/hdel.
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.
在这种情况下,你很有可能已经被 Javascript 的作用域规则击中了: requests[i] 是闭包的一部分,但闭包只能在函数级别(而不是块级别)定义.
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).
您可能需要定义一个内部函数,或者使用 forEach 来迭代您的容器.更多信息在这里:
You probably need to define an inner function, or to use forEach to iterate on your container. More information here:
nodejs、redis.检查密钥是否存在,如果不存在则创建新的
这篇关于hget 块内的 hdel nodejs redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!