Javascript 正确嵌套Redis回调

Javascript 正确嵌套Redis回调,javascript,node.js,redis,snmp,node-redis,Javascript,Node.js,Redis,Snmp,Node Redis,我正在编写一个node.js程序,它接收snmp陷阱并将它们存储在redis哈希数据库中。我在Node.js中编程的时间不长,我认为我不完全理解我的代码是如何执行的 我觉得我的一些redis调用比其他调用先完成,这就是为什么我尝试将所有调用嵌套在它们的回调函数中,但我担心我没有以正确的方式完成 我的产出看起来正接近我想要实现的目标,但有一些随机的担忧突然出现,比如我这一代人——第一次遇到陷阱是1,下一次是11或111。我所做的就是对一个整数执行redisclient.incr函数 简单地说,我使

我正在编写一个node.js程序,它接收snmp陷阱并将它们存储在redis哈希数据库中。我在Node.js中编程的时间不长,我认为我不完全理解我的代码是如何执行的

我觉得我的一些redis调用比其他调用先完成,这就是为什么我尝试将所有调用嵌套在它们的回调函数中,但我担心我没有以正确的方式完成

我的产出看起来正接近我想要实现的目标,但有一些随机的担忧突然出现,比如我这一代人——第一次遇到陷阱是1,下一次是11或111。我所做的就是对一个整数执行redis
client.incr
函数

简单地说,我使用了两个散列键来跟踪我收到的当前和过去的陷阱。我将两个数字生成和散列数作为单独的整数键值进行跟踪,并根据需要增加它们。这两个数字用于创建和访问当前和过去陷阱散列中的值

这是我的密码

function alarmCheck (key, field, value, alarmType, ipAddress) {
var historyKey = "History:"+key;
//var generationNumber;
//var numberInHash;

client.get(ipAddress+":"+field+":Gen", function (err, rep) {
    //generationNumber = rep;
    var generationNumber = rep;
    console.log("The Gen: "+rep);
    client.get(ipAddress+":"+field+":Field", function (err, reply) {

        var numberInHash = reply;
        //numberInHash = reply;
        console.log("The Field: "+reply);

        console.log("ALARM TYPE: "+alarmType);
        if(alarmType == 1) //Alarm Start value is 1
        {
            fullCurrKey = "Current:" + key;
            fullField = field + ":" + generationNumber + ":" + numberInHash;
            console.log("The FULL Field: "+fullField);
            client.hsetnx(fullCurrKey, fullField, value, function (err, status) {
                if(status == 1)
                {
                    console.log("ADDED to Current!");
                    client.incr(ipAddress+":"+field+":Field", redis.print);
                }
            })
    } else //If Alarm Start value is 0 or 2 
        {
            fullCurrKey = "Current:" + key;
            fullHistKey = "History:" + key;
            console.log("Loop generationNumber: "+generationNumber);
            console.log("Loop numberInHash: "+numberInHash);
            loop1:
            for(var i=1;i<=generationNumber;i++)
            {
                loop2:
                for(var j=1;j<=numberInHash;j++)
                {
                    fullField = field + ":" + i + ":" + j;
                    console.log("Alarm 0 or 2 fullField: "+fullField);
                    client.hget(fullCurrKey, fullField, function (err, reply) {
                        var theField = fullField;
                        if(reply == null)
                        {
                            console.log("Null hget!");
                        }
                        else
                        {
                            console.log("Adding to history!");
                            console.log("The loop hget reply: "+reply);
                            console.log("The loop hget fullField: "+fullField);
                            console.log("The loop hget theField: "+theField);
                            client.hset(fullHistKey, theField, reply, redis.print);
                            //break loop1;
                        }
                    });
                }
            }
            client.set(ipAddress+":"+field+":Field", 1, function (err, reply) {
                client.incr(ipAddress+":"+field+":Gen", function (err, reply) {
                    client.hset(fullHistKey, field+":"+generationNumber+":"+(numberInHash+1), value, function (err, reply) {
                        if (err)
                        {
                            console.log("Fail :( "+err);
                        }
                    });
                }); 
            });
                    }
    });
});
}
这是我的一些历史输出

1) "Line Length Error:1:11"
2) "Line Length Error( Error ) 1345493147"
3) "EAV Place Error:1:11"
4) "EAV Place Error( Error ) 1345493147"
5) "SAV Place Error:1:11"
6) "SAV Place Error( Error ) 1345493147"
7) "Composite Gamut Error:1:3"
8) "Composite Gamut Error( Cc ) 1345493147"
9) "Composite Gamut Error:1:31"
10) "Composite Gamut Error 1345493147"
11) "Luma Gamut Error:1:3"
12) "Luma Gamut Error( Ll ) 1345493147"
13) "Luma Gamut Error:1:31"
14) "Luma Gamut Error 1345493147"
15) "Y Anc Checksum Error:1:11"
16) "Y Anc Checksum Error( Error ) 1345493147"
17) "RGB Gamut Error:1:3"
18) "RGB Gamut Error( R--gBb ) 1345493147"
19) "RGB Gamut Error:1:31"
20) "RGB Gamut Error 1345493147"
21) "Y Anc Checksum Error:2:11"
22) "Y Anc Checksum Error( Error ) 1345493147"
23) "Line Length Error:2:11"
24) "Line Length Error( Error ) 1345493147"
25) "Field Length Error:1:11"
26) "Field Length Error( Error ) 1345493147"
27) "SAV Place Error:2:11"
28) "SAV Place Error( Error ) 1345493147"
29) "AP CRC Error:1:11"
30) "AP CRC Error( Invalid ) 1345493147"
31) "FF CRC Error:1:11"
32) "FF CRC Error( Invalid ) 1345493147"
33) "EDH Error:1:11"
34) "EDH Error( Invalid ) 1345493147"

为此,我可以向您强烈推荐。它将为代码带来更多的结构和控制,并减少嵌套回调

async.waterfall([
  function(callback){
    redis.get('abc', function(error, result) {
      callback(error, result);
    });
  },
  function(firstResult, callback){
    redis.set('abc', firstResult, function(error, result) {
      callback(error, result);
    });
  }
], function (err, result) {
   // do something to finish operations
});

嗨,罗伊瓦伦丁!你能澄清你的问题或担忧吗?不清楚被问到了什么。@SripathiKrishnan很抱歉不清楚。我只是在倾诉我的计划,没有考虑我如何很好地表达我的问题。我希望现在更清楚了。
async.waterfall([
  function(callback){
    redis.get('abc', function(error, result) {
      callback(error, result);
    });
  },
  function(firstResult, callback){
    redis.set('abc', firstResult, function(error, result) {
      callback(error, result);
    });
  }
], function (err, result) {
   // do something to finish operations
});