0
router.get('/getChefMeals/:uid', function(req, res, next) {
var userid = req.params.uid;
var allMealsIds = [];
var allMeals = [];
console.log("inside the getChefMeals/UID");
console.log("the uid is " + userid);
db.ref("chefs/" + userid).child("meals").once("value",function(dataSnapShot){
dataSnapShot.forEach(function(snap){
allMealsIds.push(snap.getKey());
});
allMealsIds.forEach(function(obj){
console.log("obj should be key of meals " + obj);
db.ref("meals/" + obj).once("value",function(dataSnapShot){
// console.log("dataSnapShot is " + dataSnapShot);
console.log("dataSnapShot key is " + dataSnapShot.getKey());
console.log("value is " + dataSnapShot.val());
allMeals.push({meal: dataSnapShot.val(), key: dataSnapShot.getKey()});
});
});
console.log("Outside allmealsloop length",allMeals.length);
res.render("detail_chef.ejs",{chefMeals: allMeals});
});
});
在上面的代碼塊,我試圖res.render
的allMeals
對象,它是JSON陣列對象直接到ejs
文件中detail_chef
。但是,在ejs
文件中,它是未定義的。快速渲染陣列對象在EJS中未定義;異步版本
的console.log
表明,由於快速路由器異步性質,console.log("Outside allmealsloop length",allMeals.length);
從第二最後一行返回0
的火力地堡DB console.log
返回所有數據就好長度和所述allMeals
是與陣列正確的長度。
我只是想知道我怎麼能通過這個數組allMeals
到ejs
?