Search code examples
mysqlnode.jsdelete-row

NodeJS MYSQL delete query


I am trying to delete a row in MYSQL table from NodeJS route. The query works fine by itself when i run it in MySQL Workbench. When executed from Node the row is not deleted. I don't get any error messages on either server or client side. Here is the code:

router.post('/bid_delete', async (req, res) => {
 let bid_no = req.body['bid_no']
 let qrBids= "SET SQL_SAFE_UPDATES = 0; DELETE FROM bids_hdr WHERE 
 bid_gen_id ='" + bid_no + "';"  
 await pool.query( qrBids, (err, result) => {
  if (err) {
    res.send(err)
  } else {
    res.json({success : true})
  }  
 });  
})

Thanks in advance for any guidance.


Solution

  • Do SET SQL_SAFE_UPDTES = 0; in a separate query.

    And use a placeholder instead of concatenation to substitute a variable into the query.

    router.post('/bid_delete', async (req, res) => {
     let bid_no = req.body['bid_no']
     let qrBids= "DELETE FROM bids_hdr WHERE bid_gen_id = ?"  
     await pool.query( "SET SQL_SAFE_UPDATES = 0");
     await pool.query( qrBids, [bid_no], (err, result) => {
      if (err) {
        res.send(err)
      } else {
        res.json({success : true})
      }  
     });  
    })