【发布时间】:2022-06-13 20:03:46
【问题描述】:
app.post("/login",function(req,res){
var name = req.body.username;
var pass = req.body.password;
Account.countDocuments({username:name, password:pass}, function(err , count){
if(count > 0){
Account.find({username:name, password:pass}, function(err,data){
prof = data[0].profile;
console.log(prof + "Executes Second"); // here it executes second
})
console.log(prof+" Executes First"); // here it executes first
res.render("index",{
url : prof,
});
}
else{
console.log("no account");
res.render("login",{
error : "You dont have a account !"
});
}
})
})
/我只是将图像 url 传递给 ejs 文件,但我想知道为什么回调函数。稍后执行/
【问题讨论】:
-
晚于什么?这就是回调的用途——当它们被传递给的函数准备好调用它们时执行的代码。
-
因为完成一件事会回调?就在名字里。
-
现在我已经用 cmets 编辑了问题
-
这意味着在 Account.find() 中,它在哪一步回调?
-
“executes first”首先执行,因为它是调用“Account.find”之后要执行的下一个代码。每当“find”调用它时,都会执行“find”的回调。我怀疑你实际上是在问异步编程一般是如何工作的。