0

In the function "Encaisser", the value of "i" is OK in the for, but if i call 'i' in a function in my function, "i" return "Undefined.

function Encaisser()
{

   for(var i=1; i <= Nombre_ligne_en_caisse; i++)
   {    
      db.transaction(function(t,i){ t.executeSql('SELECT En_cour FROM Sequence WHERE Nom="Ticket_ID"', [], function(tx,rs,i){ 

         var row = rs.rows.item(0); 
         var Tick_ID = row['En_Cour']; 
         var Noma = window['Produit_en_caisse_' + i]  ;

         alert(i); //Undefined
         alert(Noma); //Undefined

      }, [])});
   alert(i); //If i put the alert here, its OK
   }

}

Do you know why?

Thank You,

Flo
  • 29
  • 5
  • 1
    you have 3 definitions of the variable `i`, the innermost one shadowing the other two and the middle one shadowing the top. take the `i`s out of your anonymous function parameter lists. – jbabey Nov 07 '13 at 18:40
  • 1
    And then have a look at [Javascript closure inside loops - simple practical example](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Andreas Nov 07 '13 at 18:41
  • 1
    And then have a look at http://en.wikipedia.org/wiki/SQL_injection – Reinstate Monica Cellio Nov 07 '13 at 18:52

2 Answers2

0

The problem is that your inner function defines a parameter named i here:

db.transaction(function(t,i){ ...

If you intend for i to be the value from the outer function, I recommend you simply remove this parameter. It doesn't appear that db.transaction is actually providing a value for this parameter anyway. You'll probably also want to close the value of i at each iteration in a separate variable, and use that inside your function, like this:

var index = i;
db.transaction(function(t){ ...

     var Noma = window['Produit_en_caisse_' + index ];

     alert(index);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • 1
    Just removing the parameter is not going to go well, you need to close the value of `i`. This is a common mistake, see for example [this](http://stackoverflow.com/questions/2853601/scope-of-variables-in-javascript-callback-functions). – Ingo Bürk Nov 07 '13 at 18:37
0

You redefine i inside both your db.transaction callback and your t.executeSql callback. Inside your t.executeSql callback, i must be undefined.

If you want to access the value of i from the for loop, you'll need to rename those parameters in your callbacks.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • As stated on the other answer: this alone will not suffice. – Ingo Bürk Nov 07 '13 at 18:42
  • @IngoBürk - It would definitely take care of the issue of `i` being undefined which is what the OP was asking about. Since the OP never says what exactly the value should be, it's hard to know if closing over `i` will give the expected result or not. – Justin Niessner Nov 07 '13 at 18:46
  • I will eat my shoes if the OP wants unpredictable behavior that most of the times will render the loop useless. If it is what he wants, it would still be a bad idea to do it like this. – Ingo Bürk Nov 07 '13 at 18:49
  • @IngoBürk - Regardless, the question was "Do you know why? (`i` is `undefined`)" which is exactly the question I answered. – Justin Niessner Nov 07 '13 at 18:50
  • Okay, so your answer suffices to answer that question, granted. But I think it should be part of any good answer to also point out obvious big mistakes. – Ingo Bürk Nov 07 '13 at 18:51