如何在另一个函数(nodejs、mysql)中调用函数

jxct1oxe  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(332)

我需要调用一个函数并返回结果,但它不返回结果。经过一番调查,我知道这必须要有承诺。下面是代码,我想知道如何返回结果使用承诺。

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;
c9qzyr3d

c9qzyr3d1#

基于回调的方法。

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;

现在,你可以像下面这样使用它

payment.paymentDetails({order_id: 1}, function(err, details){ 
   // some code 
});

相关问题