gpt4 book ai didi

javascript - Node js异步 waterfall 未按顺序传递变量

转载 作者:行者123 更新时间:2023-12-03 05:09:01 27 4
gpt4 key购买 nike

我正在使用异步库中的 waterfall 来尝试控制我的查询和渲染过程。该过程仍然没有按照我想要的顺序进行。系列中的第二个函数未在正确的时间接收 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
});
});

最佳答案

在第一个函数中,在返回查询结果之前调用回调。如果 user.chkUserStatus 不为 true,则不会调用回调。

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);
}

关于javascript - Node js异步 waterfall 未按顺序传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41904043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com