'How to eliminate all queries if one failed

app.post('/transfer', (req,res) => {
  db.query('SELECT * FROM bank.user_info where acc_no = ?',
  [req.body.from],(err, result) => {
    if(err){
      res.send({err: err,id: 0});
    }else{
      if(req.body.Amount>result[0].acc_bal){
        res.send({err: "Insufficient Balance !!!",id: 1})
        return 0;
      }else{
        const senderNew = {};
        senderNew.amount = req.body.Amount;
        senderNew.transaction = 'sent';
        senderOld.push(senderNew);
        const freshSend = JSON.stringify(senderOld);
        const senderBal = result[0].acc_bal-req.body.Amount;
        db.query('UPDATE user_info SET transactions = ?, acc_bal = ? WHERE acc_no = ?',
        [freshSend, senderBal, req.body.from], (err,result) => {
          if(err){
            res.send({err: err,id: 2})
          }else{
            db.query('SELECT * from user_info WHERE acc_no = ?',
            [req.body.to], (err, result) => {
              if(err){
                res.send({err: err,id: 3});
              }else{
                const receiverOld = JSON.parse(result[0].transactions);
                const receiverNew = {};
                receiverNew.amount = req.body.Amount;
                receiverNew.transaction = 'received';
                receiverOld.push(receiverNew);
                const freshReceive = JSON.stringify(receiverOld);
                const receiverBal = parseInt(result[0].acc_bal) + req.body.Amount;
                db.query('UPDATE user_info SET transactions = ?, acc_bal = ? WHERE acc_no = ?',
                [freshReceive, receiverBal, req.body.to], (err, result) => {
                  if(err){
                    res.send({err: err,id: 4});
                  }else{
                    db.query('SELECT * FROM user_info WHERE acc_no = ?',
                    [req.body.from], (err, final) => {
                      if(err){
                        res.send({err: err, id: 5});
                      }else{
                        res.send(final);
                        console.log(final);
                      }
                    })
                  }
                })
              }
            })
          }
        })
      }
    }
  })
});

Now in this code, if the initial block fails, the others don't execute but if the last ones fail, the initial block still gets passed, how can I improve the code so that if any query fails, the ithers fail too...

I've checked this but it didn't helped as I need a Node.js tutorial How to stop all DB queries if one fails



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source