node express向mySql新增多条数据

//db.js

app.post('/addClient',(req,res,next) => {
  let body = req.body
  console.log('dbadd',body);
  let arr = []
  if (body.length) {
    body.forEach(ele => {
      if (typeof ele == 'object') {
        let par = []
        for (let key in ele) {
          if (ele[key]) {
            par.push(ele[key])
          }
        }
        arr.push(par)
      }
    });
  }
  // res.send({code: 200, msg: '成功'})
  db.dbAdd('client',arr,res,next)
})


function dbAdd(table, req, res, next) {
    pool.getConnection((err, connection) => {
        connection.query(sql[table].insert, [req], (err, result) => {
            console.log('reqqqq',req);
            console.log('mmmm',[req]);
            if (result) {
                result = 'add'
            }
            // 返回结果
            json(res, result);
            // 释放连接
            connection.release();
        })
    })
}

  

posted @ 2022-10-19 15:56  小羊杨杨  阅读(291)  评论(0编辑  收藏  举报