节点js异步瀑布不按顺序传递变量

时间:2017-01-27 22:26:29

标签: javascript sql node.js asynchronous

我正在使用异步库中的瀑布来尝试控制我的查询和渲染过程。这个过程仍然没有按照我想要的顺序运行。该系列中的第二个函数未在正确的时间接收qryUser参数。

    async.waterfall([
    function (callback){
                      if(user.chkUserStatus){

                        if(user.lWaterLabID == 0){
                          qryUser.sLabName="Site Admin";
                          qryUser.sLabID=0;
                        }
                        else if(user.lWaterlabID == -9999){

                            qryUser.sLabName = "Uber Admin";
                            qryUser.sLabID = "0";



                        }
                        else{

    request.query("SELECT Someqry",
 function(err,recordset){


    qryUser.sLabName = recordset[0].sLabName;
    qryUser.sLabID = recordset[0].LabID;

});


                                        }
    callback(null,qryUser);
                    },

下面函数中的qryUser为空,并且在设置变量之前正在记录。

function(qryUser, callback) {
            console.log('qryUser in setTables',qryUser);
            request.query("Someqry",function(err,recordset){
            console.log('qryFindLocalID',recordset);

            callback(null,recordset);
                                                        });

}

    }],
function (err, result) {
    console.log('the waterfall is finished results are',result);
    res.render('secure/index',{
        user:user,
        qryUser:qryUser
    }); 
});

1 个答案:

答案 0 :(得分:1)

在第一个函数中,在返回查询结果之前调用回调。如果user.chkUserStatus不是真的,也不会调用回调。

async.waterfall([
  function (callback) {
    if (user.chkUserStatus) {
      if (user.lWaterLabID == 0) {
        qryUser.sLabName="Site Admin";
        qryUser.sLabID=0;
        // callback here
        callback(null,qryUser);
      } else if (user.lWaterlabID == -9999) {
        qryUser.sLabName = "Uber Admin";
        qryUser.sLabID = "0";
        // callback here
        callback(null,qryUser);
      } else {
        request.query("SELECT Someqry",
          function (err,recordset) {
            qryUser.sLabName = recordset[0].sLabName;
            qryUser.sLabID = recordset[0].LabID;
            // callback here
            callback(null,qryUser);
          }
        );
      }
    }
    // Houston we've got a problem ...
    // user.chkUserStatus is not truthy
    var myError = new Error('My user.chkUserStatus is not truthy error!');
    callback(myError);
  },
  function(qryUser, callback) {
    console.log('qryUser in setTables',qryUser);
    request.query("Someqry", function(err,recordset) {
      console.log('qryFindLocalID',recordset);
      callback(null,recordset);
    });
  }
],
function (err, result) {
  console.log('the waterfall is finished results are',result);
  res.render('secure/index',{
    user:user,
    qryUser:qryUser
  }); 
});

第一个函数看起来有点讨厌所有嵌套逻辑,下面的代码更容易阅读。

function (callback) {
  if (user.chkUserStatus) {
    if (user.lWaterLabID == 0 || user.lWaterlabID == -9999) {
      // in both cases sLabID is set to 0
      qryUser.sLabID = 0;
      // assign the correct value for sLabName
      qryUser.sLabName = (user.lWaterLabID == 0) ? "Site Admin" : "Uber Admin";
      // callback here
      callback(null,qryUser);
    } else {
      request.query("SELECT Someqry", function (err,recordset) {
        // should check the err parameter
        if (err) { callback(err); }
        // we have a result
        qryUser.sLabName = recordset[0].sLabName;
        qryUser.sLabID = recordset[0].LabID;
        // callback here
        callback(null,qryUser);
      });
    }
  }
  // Houston we've got a problem ...
  // user.chkUserStatus is not truthy
  var myError = new Error('My user.chkUserStatus is not truthy error!');
  callback(myError);
}