2017-11-30 1 views
1

js 이제 내 app.js에 문제가 있음 루트 루트가 실행되기 전에 문제가 발생했습니다. 다음과 같이 호출 할 함수가 있습니다 :node.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; 
} 

는 데이터베이스에서 범주를 찾아 내 경로 루트 이것이) (광고에

app.get("/",function(req, res){ 

      res.render("landing",{cats: findCats()}); 

}); 

감사 을 res.render을 실행하기 전에이 결과를 사용해야 결과에게 반환에게 경로 루트를 보내 밴스!

답변

0

는 그 다음 일했다

app.get("/",function(req, res){ 
    findCats((cats) => { 
     res.render("landing", {cats}); 
    }); 
}); 
+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); } }); } 

에 콜백을 추가! –