-1

i need to call a function and return the result but it does not return the result. After some search i got to know that it has to be done with promises. Below is the code and i would like to know how to return the result using promises.

var payment = {
paymentDetails: function(data){
    var  order_id = data.order_id;

    db.query("select o.sku,p.published_on,sp.total_sale_price as amount, o.total_cost from orders o left join product p on p.sku = o.sku and p.is_published = 1 and p.is_deleted = 0 left join supplier_product sp on sp.product_id = p.product_id and sp.is_deleted = 0 and sp.active = 1 where o.order_id = ? and o.order_status < 13",[order_id], function(err, res){
        if(err) return err;

        var order_details = res[0];
        var payment_details  = module.exports.getPaymentDetails(order_id);
    });

},

getPaymentDetails: function(order_id){
    db.query("select payment_type, amount, status from payment_details where payment_mode = 5 and invoice_type = 8 and status in ('success','pending_for_approval') and merchant_order_id = ?",[order_id], function(err, res){
      if(err){ 
        throw err;
      } else {
        return res;
      }          
  });  
} 
};
module.exports = payment;
r93
  • 1
  • 1

1 Answers1

1

Callback based approach.

var payment = {
    paymentDetails: function(data, callback){
        var  order_id = data.order_id;

        db.query("somequery",[order_id], function(err, res){
           if(err) return callback(err, null);

           var order_details = res[0];
           var payment_details = module.exports.getPaymentDetails(order_id, callback);
        });

    },

    getPaymentDetails: function(order_id, callback){
         db.query("somequert",[order_id], function(err, res){
            if(err){ 
               return callback(err, null);
            } else {
               return callback(null, res);
            }          
         });  
    } 
};

module.exports = payment;

Now, you can use it like the following

payment.paymentDetails({order_id: 1}, function(err, details){ 
   // some code 
});
Mukesh Sharma
  • 8,914
  • 8
  • 38
  • 55