异步并行:如何解析结果

时间:2017-06-18 15:08:31

标签: node.js async.js

我尝试计算这种情况下的总销售价格,但它不起作用。我正确使用async.parallel吗?如何将计算结果(totalSalesResult)解析为totalSales?

async.parallel({
 totalSales: function(totalSales) {
        Sales.find({merchant: req.user.id}).exec(function(err, results) {
            var totalShippingFees = 0;
            var totalItemSales = 0;
            for(var i = 0; i < results.length; i++) {
                totalShippingFees += parseFloat(results[i].shippingFees);

                var items = results[i].items;
                for(var j = 0; j < items.length; i++) {
                    totalItemSales = parseFloat(items[j].sellingPrice);
                }

            }
            var totalSalesResult = parseFloat(totalShippingFees + totalItemSales);
            totalSales(null, totalSalesResult)
        });
    }
}, function(err, results) {
    console.log(results.totalSales)
});

0 个答案:

没有答案