2017-09-20 91 views
0

我有以下代碼:節點不是等待回調

module.exports.getFinancialDashboard = function(req,res){ 
    console.log("getFinancialDashboard");  
    if(req && req.query.shopSlug){ 
     shopService.getShopFromSlug(req.query.shopSlug, function(err, shop){ 
      var status, response; 

      var outStandingOrders = 0; 
      var nextPaymentDate = new Date(); 
      var nextPayment =0; 
      var revenue = 0; 
      var expectedRevenue = 0; 
      var orderCount =0; 

      if(err) { 
       status = 500; 
       response = {message: 'Internal server error. ' + err}; 
       return res.status(status).json(response); 
      } 

      Payment.find({shopId: shop._id}).exec(function(err, payments){ 
       if(err) { 
        status = 500; 
        response = {message: 'Internal server error. ' + err}; 
        return res.status(status).json(response); 
       } 

       async.forEachOf(payments, function(payment, i, paymentDone){ 
        parsePayment(payment, function(err, paymentRevenue, paymentExpectedRevenue){ 
         orderCount= i+1; 
         if(err){ 
          console.log("ERROR:", err); 
         } 
         if(paymentRevenue){ 
          revenue = revenue + paymentRevenue; 
         } 
         if(paymentExpectedRevenue){ 
          expectedRevenue = expectedRevenue + paymentExpectedRevenue; 
          outStandingOrders++; 
         } 
        }) 

        paymentDone(); 
       }, function(){ 

        console.log("revenue", revenue); 
        console.log("expected", expectedRevenue); 
        console.log("ordercount", orderCount, "paymentslength", payments.length); 
        if(orderCount == payments.length){ 
         var dashboard = { 
          outStandingOrders: outStandingOrders, 
          nextPaymentDate: nextPaymentDate, 
          payment: nextPayment, 
          revenue: revenue, 
          expectedRevenue: expectedRevenue 
         } 
         console.log("DAS", dashboard); 

         status = 200; 
         response = {message: 'OK', dashboard}; 
         return res.status(status).json(response); 
        } 
       }) 
      }) 
     }) 
    }else{ 
     return res.status(500).json({message: 'No details provided'}); 
    } 
} 


function parsePayment(payment, callback){ 

    var revenue = 0, expectedRevenue =0; 

    if(payment.amount > 0){ 
     if(payment.completed && payment.exported){ 
      console.log("completed payment"); 
      return callback(null, payment.amount); 
     }else{ 
      Order.findOne({_id: payment.orderId}).exec(function(err, order){ 
       if (err){ 
        return callback(err); 
       } 
       if(orderService.getOrderStatus(order) == "Send"){ 
        console.log("sended order"); 
        return callback(null, null, payment.amount); 
       }else{ 
        return callback(); 
       } 
      }) 
     } 
    } 
} 

我想將所有發送回之前所分析的數據,但在的NodeJS儘管沒有回調等待。我得到了我的控制檯如下:

completed payment 
completed payment 
completed payment 
completed payment 
completed payment 
completed payment 
completed payment 
completed payment 
completed payment 
completed payment 
completed payment 
revenue 3022.6799174 
expected 0 
ordercount 23 paymentslength 24 
sended order 

正如你所看到的,我得到的收入「,「預期」和之前的最後一個」定單計數「sended秩序」。 因爲這樣,ordercount不等於payments.length,所以我從來沒有收到結果。

我已經嘗試了異步庫中的不同功能,如forEach和ForEachSeries,一個簡單的for循環,沒有回調,但是這不起作用。

我認爲這是一個簡單的錯誤我做了,但我不能找到它..

回答

2

我覺得你的代碼沒有等到最後一個,因爲parsePayment是異步函數調用paymentDone它之前完成。

你需要把paymentDone在parsePayment回調函數

module.exports.getFinancialDashboard = function(req,res){ 
    console.log("getFinancialDashboard");  
    if(req && req.query.shopSlug){ 
     shopService.getShopFromSlug(req.query.shopSlug, function(err, shop){ 
      var status, response; 

      var outStandingOrders = 0; 
      var nextPaymentDate = new Date(); 
      var nextPayment =0; 
      var revenue = 0; 
      var expectedRevenue = 0; 
      var orderCount =0; 

      if(err) { 
       status = 500; 
       response = {message: 'Internal server error. ' + err}; 
       return res.status(status).json(response); 
      } 

      Payment.find({shopId: shop._id}).exec(function(err, payments){ 
       if(err) { 
        status = 500; 
        response = {message: 'Internal server error. ' + err}; 
        return res.status(status).json(response); 
       } 

       async.forEachOf(payments, function(payment, i, paymentDone){ 
        parsePayment(payment, function(err, paymentRevenue, paymentExpectedRevenue){ 
         orderCount= i+1; 
         if(err){ 
          console.log("ERROR:", err); 
         } 
         if(paymentRevenue){ 
          revenue = revenue + paymentRevenue; 
         } 
         if(paymentExpectedRevenue){ 
          expectedRevenue = expectedRevenue + paymentExpectedRevenue; 
          outStandingOrders++; 
         } 
         paymentDone(); // <-- move to callback 
        }) 

       }, function(){ 

        console.log("revenue", revenue); 
        console.log("expected", expectedRevenue); 
        console.log("ordercount", orderCount, "paymentslength", payments.length); 
        if(orderCount == payments.length){ 
         var dashboard = { 
          outStandingOrders: outStandingOrders, 
          nextPaymentDate: nextPaymentDate, 
          payment: nextPayment, 
          revenue: revenue, 
          expectedRevenue: expectedRevenue 
         } 
         console.log("DAS", dashboard); 

         status = 200; 
         response = {message: 'OK', dashboard}; 
         return res.status(status).json(response); 
        } 
       }) 
      }) 
     }) 
    }else{ 
     return res.status(500).json({message: 'No details provided'}); 
    } 
} 


function parsePayment(payment, callback){ 

    var revenue = 0, expectedRevenue =0; 

    if(payment.amount > 0){ 
     if(payment.completed && payment.exported){ 
      console.log("completed payment"); 
      return callback(null, payment.amount); 
     }else{ 
      Order.findOne({_id: payment.orderId}).exec(function(err, order){ 
       if (err){ 
        return callback(err); 
       } 
       if(orderService.getOrderStatus(order) == "Send"){ 
        console.log("sended order"); 
        return callback(null, null, payment.amount); 
       }else{ 
        return callback(); 
       } 
      }) 
     } 
    } 
} 
+0

嗯,當我做到這一點,他從來沒有調用異步回調函數()。所以他甚至不會嘗試發送結果 – NVO

+0

parsePayment中是否有任何錯誤嘗試放置一些console.logs並檢查回調被調用的次數 – David

+0

它看起來像回調被稱爲23次付款的時間長度。我註釋掉了if(ordercount == payments.length),但他並沒有觸發整個功能... – NVO