jeudi 7 juillet 2016

Nodejs multiple sql query loop

I am pretty new to nodejs and async worlds.

The case is, I have an array like var ids = [1, 2, 3, 4];

I need to update mytable according to sequence of the array element. So I do something like:

sort: function(ids, callback) {

 // dont worry about this
  this.create_connection();
  this.connection.connect();

   for(var i=0; i<ids.length;i++) {
      var q = "UPDATE mytable SET sequence="+i+" where id="+ids[i]+"; ";

      this.connection.query(q, function(err, result) {

       // I am not sure about this
       // callback(err);
     });
   }

 // I need to return callback at the end
 // return callback();

  this.connection.end();

}

But yes.. it does not work because I have to return callback.. I think I need to do the query syncronously.. I am not sure. Please help thanks.

Aucun commentaire:

Enregistrer un commentaire