js,现在我的app.js出现了问题 问题是我想在执行路由根之前调用一个函数 我想打电话的功能如下:
function findCats(){
var cats;
Categorie.find({}, function(err, foundCats){
if(err){
console.log("Error can not find the categories ==> "+err);
}else{
this.cats = foundCats;
}
});
return this.cats;
}
它从数据库中找到一个类别并发送返回结果 在执行res.render()之前,路由根应该使用此结果 这是我的路线根
app.get("/",function(req, res){
res.render("landing",{cats: findCats()});
});
提前感谢!
答案 0 :(得分:0)
为您的findCats功能添加回调
function findCats(callback) {
var cats;
Categorie.find({}, function(err, foundCats){
if (err) {
console.log("Error can not find the categories ==> " + err);
} else {
callback(foundCats);
}
});
}
然后在回调中发送您的回复
app.get("/",function(req, res){
findCats((cats) => {
res.render("landing", {cats});
});
});