正确嵌套Rediscallback

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

我觉得我的一些redis调用在其他调用之前完成,这就是为什么我试图在callback函数中嵌套一切,但是我担心我没有按照正确的方式来执行。

我的输出看起来就像我正在接近我想要达到的目标,但是还有一些随机的问题,比如我的Generation#为1,我接收到第一个陷阱,然后是11或111。 我所做的就是在整数上执行redis client.incr函数。

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

这是我的代码。

 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) "Composite Gamut Error:1:1" 2) "Composite Gamut Error( -c ) 1345493146" 3) "Luma Gamut Error:1:1" 4) "Luma Gamut Error( -l ) 1345493146" 5) "Jitter1 Level:1:1" 6) "Jitter1 Level 1345493146" 7) "RGB Gamut Error:1:1" 8) "RGB Gamut Error( Rr-gBb ) 1345493146" 9) "Composite Gamut Error:1:2" 10) "Composite Gamut Error( Cc ) 1345493147" 11) "Luma Gamut Error:1:2" 12) "Luma Gamut Error( Ll ) 1345493147" 13) "RGB Gamut Error:1:2" 14) "RGB Gamut Error( R--gBb ) 1345493147" 15) "SDI Input Signal Lock:1:1" 16) "SDI Input Signal Lock( Unlocked ) 1345493147" 17) "Y Anc Checksum Error:3:1" 18) "Y Anc Checksum Error( Error ) 1345493147" 19) "SDI Input Signal Lock:2:1" 20) "SDI Input Signal Lock( Unlocked ) 1345493147" 21) "Line Length Error:4:1" 22) "Line Length Error( Error ) 1345493147" 23) "SAV Place Error:4:1" 24) "SAV Place Error( Error ) 1345493147" 25) "AP CRC Error:3:1" 26) "AP CRC Error( Invalid ) 1345493147" 27) "FF CRC Error:3:1" 28) "FF CRC Error( Invalid ) 1345493147" 29) "EDH Error:3:1" 30) "EDH Error( Invalid ) 1345493147" 

这是我的一些历史输出。

 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" 

我可以强烈推荐这个asynchronous瀑布 。 它会给你的代码带来更多的结构和控制,并减less嵌套的callback。

 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 });