title: How to rollback when error occurred in the second query execution| NodeJS
body: <p>I used to use rollback in <code>PHP</code>, I want to implement the same concept in <code>NodeJS</code>. I am calling a function that inserts a data to the <code>database</code>, and on success I am calling another function from another <code>Model</code> to insert data into another <code>table</code>.</p>
  
  <p><strong>My Code</strong></p>
  
  <p><strong><code>StoreReport</code></strong></p>
  
  <pre><code>'use strict';
  
  var sql = require('./db.js');
  
  // IMPORT ANOTHER MODEL
  var CashDetailModel = require('./cashDetailModel.js');
  
  var StoreReport =function(storeReport){
      this.store_id=storeReport.store_id;
      this.cash_expense=storeReport.cash_expense_amount;
  
  }
  
  StoreReport.addNewStoreReport = function (report_details,request,result){
  
      sql.query('INSERT INTO store_report SET ?',report_details, function(err,res){
          if(err){
              result(err,null);
          }else{
              var new_cash_detail = new CashDetailModel(report_details);
              new_cash_detail.store_report_id=res.insertId;
              CashDetailModel.addCashDetail(new_cash_detail,function(err,CashDetailModel){
                  if(err){
  
                    // I NEED TO ROLLBACK IF ERROR OCCURED IN THIS PHASE
                      res.send(err);
                  }else{
                      result(null,res.inserted);
                  }
              })
          }
      })
  }
  
  module.exports = StoreReport;
  </code></pre>