async-series TypeError:无法读取属性' 0'未定义的

时间:2018-06-05 09:40:16

标签: node.js

获取标题中提到的错误在calculateGST行total = req.itemdetail [0] .price;

当我尝试在getItemDetail()内部打印相同的内容时获取正确的值。我相信这与异步行为有关。如何在不使用瀑布或任何其他包的情况下使其像同步一样工作?我想要系列包的解决方案。

 exports.calculateGstBaseOnInput = function(req, res,next) {
        for(var item of req.body.so_items) {
            req.productid = item.productid;
            req.qty = item.qty;
            series([function(done){
                getItemDetail(req,res);
                done();
                getCartItems(req,res);
                done();
                calculateGST(req,res);
                done();
              }], function(err) {
                //console.log(err) // "another thing"
              })
         }
       // res.json({status : 'gst', statusMessage : 'gst success'});

    }

    getItemDetail =function(req,res)
    {
        var SQL = "mysqlquery";
        mysqlConnect.query(SQL,function(err, result, fields){
                    if(err)
                    {

                        res.json({status : 'Failure', statusMessage : 'item does not exist'});
                    }else if(result.length < 0){
                        res.json({status : 'Failure', statusMessage : 'item does not exist'});
                    }
                    else
                    {
                        req.itemdetail = result;
                    }
                });
    }
    getCartItems =function(req,res)
    {

        var SQL="mysqlquery";
        mysqlConnect.query(SQL,function(err, result, fields){
                    if(err)
                    {
                        res.json({status : 'Failure', statusMessage : 'item does not exist'});
                    }else if(result.length < 0){
                        res.json({status : 'Failure', statusMessage : 'item does not exist'});
                    }
                    else
                    {
                        req.cartItems = result;
                    }
                });
    } 
    calculateGST =function(req,res)
    {

            total = req.itemdetail[0].price;

    } 

1 个答案:

答案 0 :(得分:0)

我猜你必须将done callback传递给functions数组中的后续async-series。我修改了代码,你能检查一下这个代码是否适合你吗?

 exports.calculateGstBaseOnInput = function(req, res, next) {
     for (var item of req.body.so_items) {
         req.productid = item.productid;
         req.qty = item.qty;
         series([function(done) {
                 getItemDetail(req, res, done);
             },
             function(done) {
                 getCartItems(req, res, done);
             },
             function(done) {
                 calculateGST(req, res, done);
             }
         ], function(err) {
             console.log(err) // "another thing"
         })
     }
     // res.json({status : 'gst', statusMessage : 'gst success'});

 }

 getItemDetail = function(req, res, cb) {
     var SQL = "mysqlquery";
     mysqlConnect.query(SQL, function(err, result, fields) {
         if (err) {

             res.json({
                 status: 'Failure',
                 statusMessage: 'item does not exist'
             });
         } else if (result.length < 0) {
             res.json({
                 status: 'Failure',
                 statusMessage: 'item does not exist'
             });
         } else {
             req.itemdetail = result;
             cb();
         }
     });
 }
 getCartItems = function(req, res, cb) {

     var SQL = "mysqlquery";
     mysqlConnect.query(SQL, function(err, result, fields) {
         if (err) {
             res.json({
                 status: 'Failure',
                 statusMessage: 'item does not exist'
             });
         } else if (result.length < 0) {
             res.json({
                 status: 'Failure',
                 statusMessage: 'item does not exist'
             });
         } else {
             req.cartItems = result;
             cb();
         }
     });
 }
 calculateGST = function(req, res, cb) {

     total = req.itemdetail[0].price;

     //After all success operation call the callback
     cb();

 }