How to solve insert array element in to DB via nested query

27 Views Asked by At

i'm using node-mysql,

var arr= ["usr1""usr2""usr3""usr4"];

for(var i=0;i<arr.length;i++){
 var q1 = "select userid from users where email=?";
 con.query(q1, [email], function(error1, rows1, fields) {
  if(error1) {
   console.info("error select users users ",JSON.stringify(error1))           
   callbackError({"response":"error", "responseString": JSON.stringify(error1)});  
  } 
  if(rows1.length==0){
   
   var q2 = "insert into users (name) values (?)";
   //console.info(arr[i])//here arr[i] returns last array element "usr4"
   con.query(q2, [arr[i]], function(error, rows, fields) {
    if(error) {
     callbackError({"response":"error"});  
    } else {
      callbackSuccess({"response":"success"});
    }
   });
  }
 });
}

here i'm getting last element to insert, how to solve this issue? please help me solve this issue

0

There are 0 best solutions below