for(i in req.body.category_id){
db.query('INSERT INTO guide_categories(category_id) values($1)', [req.body.category_id[i]],function(err,category) {
  if(err) return next(err);                         
  return console.log("success");           
  });                             
}
Here I am passing the array of values from postman as category_id:["3","1"], but the last value of the array is got inserting and rest is not. How to solve this??

share|improve this question
    
how do you know its not inserted?.. db.query is async?.. – Vao Tsun 19 hours ago
    
I am checking database every time. – M gowda 19 hours ago
    
Multiple inserts should be done via a single, multi-insert query. Example: Multi-row insert. – vitaly-t 12 hours ago
up vote 0 down vote accepted

change to

for(i in req.body.category_id){
db.query('INSERT INTO guide_categories(category_id) values($1) returning *', [req.body.category_id[i]], (err,category) => {
  if(err) {
    console.log(err);     
    } else {                    
    console.log(category);   
  }        
  };                             
}

and populate the output of console.log

share|improve this answer
    
thank you, it works – M gowda 18 hours ago
    
but I don't know where I am wrong. – M gowda 18 hours ago
    
I assume you return after first iteration?.. not going for next results – Vao Tsun 18 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.