For循环不在Model.find()中循环

时间:2014-08-22 14:07:04

标签: javascript node.js for-loop express mongoose

我有for循环的问题。 在DEPOSIT.find(....)函数中,for循环无法正常工作。它在数组中提取仅最后一个对象。控制台打印这个:

TX 5b32f392512404e3319dac2f72a2a0c42a90fe4042cbdea5a6779b5d20a71b02 receive 0.00011719
TX 5b32f392512404e3319dac2f72a2a0c42a90fe4042cbdea5a6779b5d20a71b02 receive 0.00011719
TX 5b32f392512404e3319dac2f72a2a0c42a90fe4042cbdea5a6779b5d20a71b02 receive 0.00011719

但如果我将console.log放在DEPOSIT.find(...)方法之外,它会打印出我想要的内容:

TX 0bd0ac0cd357d3fc4486db6861653c1c10cd119c524098fa761076575126f7fa receive 0.00195312
TX f264cf087a3aded73c404df4d6a6915b19859986787f15de6261058006d435e8 receive 0.00295312
TX 5b32f392512404e3319dac2f72a2a0c42a90fe4042cbdea5a6779b5d20a71b02 receive 0.00011719

我错过了什么?我认为问题是mongoose's .find方法?


代码:

var DEPOSIT = require('./DataModels/_deposits');
function DepositKontrol () {

client.listTransactions('*', 1000, function(err, list, resHeaders) {
      if (err) return console.log(err);
        var depositolar = list;
          for (i = 0; i < depositolar.length; i++) { 
            if(depositolar[i].category == "receive"){   
              if(depositolar[i].address !=null){
                 if(depositolar[i].amount>0.00000001){      
                    dep.find({'deposit.txid' : depositolar[i].txid}, function (err, docs) { 

                       if(docs.length==0){
                        console.log("no txid like this, insert new ");
                            //OK
                        }
                        else {
                        console.log("tx id var, confirmation update");  
                            var query = {'deposit.txid' : depositolar[i-1].txid};
                            DEPOSIT.update(query, { 'deposit.confs': depositolar[i-1].confirmations },   function (err, statu) { 
         ------->//PROBLEM  console.log("TX "+depositolar[i-1].txid+" "+depositolar[i-1].category+" "+depositolar[i-1].amount);

               });
             }
          }); 
        }
      }
    }   
  }
});

}

0 个答案:

没有答案