title: Issue inserting values in a loop (for) in the database : same value is inserted - node js / sql
body: <p>I try to insert some data into a database inside a loop. The issue is that it inserts always the same value (the first one). I mean even if the value of my variable is different (I display it on the console so I can check that everything is ok before querying the database to insert it).</p>
  
  <p>My program is based on Node.js.</p>
  
  <p>Here is an example of my code : </p>
  
  <pre><code>var obj = JSON.parse(result.body);
  global.store_urls = [];
  
  for (let i = 0; obj.productEntities.length &gt; i; i++){
  
     global.store_urls.push(obj.productEntities[i].url);
     var reference=obj.productEntities[i].IdProduct;
     var brand=obj.productEntities[i].Brand;
     var price=obj.productEntities[i].Price;
     var new_price=obj.productEntities[i].PriceNew;
     var reduction=obj.productEntities[i].sale_state;
     var type_1=obj.productEntities[i].Category;
     var sold=obj.productEntities[i].sold;
  
     console.log(reference+" / "+brand+" / "+price+" / "+new_price+" / "+reduction+" / "+type_1+" / "+sold);
  
     //Row (data) insertion part
     var sql = mysql.format("SELECT * FROM vd_tendance WHERE url=?", [obj.productEntities[i].url]);
     var selectUrl = connection.query(sql, function(err,rows,fields) {
  
      if(err){throw err;}
      //Checking the value exist on the table
      if(rows.length &gt; 0){
       //If already exists, no need to insert
        console.log(rows.length);
      }else{
       //Save to database
       connection.query('INSERT INTO `vd_tendance` VALUES ( NULL , ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP())',
                    [ reference,
                      type_1,
                      brand,
                      price,
                      new_price,
                      reduction,
                      sold,
                      url,
                    ]
                , function (err, result) {
                    if (err) {console.error('error inserting into database : ' + err.stack); return;}
                    else{}
                });
              }
            });
  
  
        }
  </code></pre>
  
  <p>The result is always the same data inserted on each row (the first value that variables have taken).</p>
  
  <p>Could you give me few ideas to solve that ? Maybe it's totally related to the asynchronous node js system ?</p>
  
  <p>Thank you all !</p>