Javascript 嵌套回调异步运行
我如何克服这个异步代码,并在回调之间传递值 此处totalfee变量在第一次回调中设置,但在第二次回调中不可访问Javascript 嵌套回调异步运行,javascript,node.js,asynchronous,mongoose,Javascript,Node.js,Asynchronous,Mongoose,我如何克服这个异步代码,并在回调之间传递值 此处totalfee变量在第一次回调中设置,但在第二次回调中不可访问 var totalfee; SetFee.find({standard:standard}, function(error,document){ totalfee = document[0].totalfee; CollectFee.find({ssn:ssn}, function(error,document){ if(typeof(document[0
var totalfee;
SetFee.find({standard:standard}, function(error,document){
totalfee = document[0].totalfee;
CollectFee.find({ssn:ssn}, function(error,document){
if(typeof(document[0])=='undefined'){
let paidfee=0;
var cfee = new CollectFee({
ssn:ssn,
paidfee:paidfee
});
cfee.save();
}
else {
showpaid=document[0].paidfee;
console.log(ssn,name,totalfee,showpaid);
res.redirect('/fees/cfees?ssn='+ssn+'&name='+name+'&totalfee='+totalfee+'&paidfee='+showpaid);
}
});
});
也许这些指针会指引你去某个地方 (1) 使用
findOne
,因为您需要一个文档
(2) 为变量使用更好的名称,例如,文档
不明确
(3) 添加用于调试的console.log
语句
(4) 最好也检查一下err
,看看是否有错误出现。通常只有在插入/更新/删除时才会发生错误
SetFee.findOne({ standard: standard }, function (err, setFee) {
console.log('setFee', setFee)
let totalfee = setFee.totalfee;
CollectFee.findOne({ ssn: ssn }, function (err, collectFee) {
console.log('collectFee', collectFee)
if (!collectFee) {
// use of return is to stop the 'else' part from executing
return CollectFee.create({ ssn: ssn, paidfee: 0 }, function (err, collectFee) {
// do something here or your route handler will hang!
});
}
let showpaid = collectFee.paidfee;
console.log(ssn, name, totalfee, showpaid);
res.redirect('/fees/cfees?ssn=' + ssn + '&name=' + name + '&totalfee=' + totalfee + '&paidfee=' + showpaid);
});
});
也许这些指针会指引你去某个地方 (1) 使用
findOne
,因为您需要一个文档
(2) 为变量使用更好的名称,例如,文档
不明确
(3) 添加用于调试的console.log
语句
(4) 最好也检查一下err
,看看是否有错误出现。通常只有在插入/更新/删除时才会发生错误
SetFee.findOne({ standard: standard }, function (err, setFee) {
console.log('setFee', setFee)
let totalfee = setFee.totalfee;
CollectFee.findOne({ ssn: ssn }, function (err, collectFee) {
console.log('collectFee', collectFee)
if (!collectFee) {
// use of return is to stop the 'else' part from executing
return CollectFee.create({ ssn: ssn, paidfee: 0 }, function (err, collectFee) {
// do something here or your route handler will hang!
});
}
let showpaid = collectFee.paidfee;
console.log(ssn, name, totalfee, showpaid);
res.redirect('/fees/cfees?ssn=' + ssn + '&name=' + name + '&totalfee=' + totalfee + '&paidfee=' + showpaid);
});
});
我知道它应该是可访问的。设置它时检查它的值。它必须是可访问的,不管它是否有指定的值是另一个故事。如果指令一个接一个地执行,它必须是可访问的,但是在totalfee=document[0]之前,其他指令被执行。totalfeecould@Adder我认为这是一个地狱的例子。你知道怎么解决这个问题吗?你有没有把
console.log(“totalfee设置为”,totalfee)放进去代码>在totalfee=文档[0]之后。totalfee代码>如何访问它。设置它时检查它的值。它必须是可访问的,不管它是否有指定的值是另一个故事。如果指令一个接一个地执行,它必须是可访问的,但是在totalfee=document[0]之前,其他指令被执行。totalfeecould@Adder我认为这是一个地狱的例子。你知道怎么解决这个问题吗?你有没有把console.log(“totalfee设置为”,totalfee)放进去代码>在totalfee=文档[0]之后。totalfee代码>干净的代码!但这并没有解决我的问题。谢谢你的努力@毗瑟奴杰夫解释。console.log
是否产生正确的输出?它挂着吗?它在哪里失败?如果在第一次回调中输入console.log(totalfee),它将返回预期的输出,但在第二次回调中它将返回null。@Vishnudev。。。totalfee
的范围应在第一次回调(包括第二次回调)期间可访问。你什么都没做,对吧?首先尝试将let
更改为var
。如果运气不好,那么试着在第二次回调中执行console.log(setFee.totalfee)
。实际上,我已经试过了你之前说的所有内容。没有成功!你知道我如何在Async.js中实现它吗?干净的代码!但这并没有解决我的问题。谢谢你的努力@毗瑟奴杰夫解释。console.log
是否产生正确的输出?它挂着吗?它在哪里失败?如果在第一次回调中输入console.log(totalfee),它将返回预期的输出,但在第二次回调中它将返回null。@Vishnudev。。。totalfee
的范围应在第一次回调(包括第二次回调)期间可访问。你什么都没做,对吧?首先尝试将let
更改为var
。如果运气不好,那么试着在第二次回调中执行console.log(setFee.totalfee)
。实际上,我已经试过了你之前说的所有内容。没有成功!你知道我如何在Async.js中实现这一点吗?