javascript - how can i get my local variable in loop to change global variable in node js using express framework ?? -


i have object array results parallel asynchronus callback, next process want insert objact array results table using looping for, dont handle status insert.

var boolean = false;    if(results.code.success === true){     for(i=0; i<req.body.id_barang.length; i++){       detail[i] = {         "id_transaksi" : results.code.id_transaksi,         "id_barang" : req.body.id_barang[i],         "qty" : req.body.qty[i],         "sub_total" : parseint(req.body.sub_total[i].replace(/,.*|\d/g,''),10)       }       connection.query("insert detail_transaksi set ? ", detail[i], function(err, rows){         if(err){           null         } else{           boolean = true;         }       })     }   }   var output = {"success" : boolean}   res.send(output); 

to expand on jfriend00's answer, function(err, rows) callback function execute once query resolved. meanwhile, rather waiting query resolve, var output = {"success" : boolean} executed without boolean modification in callback.

so, in order send output, must include code in callback, after boolean = true;.

here's great question giving basic example of callback usage in node.js.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -