0

I have a problem with for loop. In DEPOSIT.find(....) function, for loop is not working properly. It's fetching only last object in array. Console is printing this:

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

But if I put console.log to out of DEPOSIT.find(...) method, it's printing what I want:

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

What is the thing is I'm missing? I think the problem is mongoose's .find method?


Code:

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

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

}
Lazy
  • 1,807
  • 4
  • 29
  • 49
  • 1
    You are closing over your `i` variable which is changing before the `console.log`. Take a look at this question http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example – RobH Aug 22 '14 at 14:18
  • Thanks, the problem should be that. – Lazy Aug 22 '14 at 14:25

0 Answers0