如何使用nodejs在postgresql中插入值数组

问题描述 投票:0回答:1

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??
javascript arrays node.js postgresql postman
1个回答
0
投票

改成

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);   
  }        
  };                             
}

并填充console.log的输出

© www.soinside.com 2019 - 2024. All rights reserved.