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});
});
});
、私はJSONの配列detail_chef
でejs
ファイルに直接オブジェクトであるallMeals
res.render
オブジェクトをしようとしています。ただし、ejs
ファイルでは未定義です。Express Rendering ArrayオブジェクトはEJSでは未定義です。非同期発行上記のコードブロックに
console.log
起因明示ルータの非同期の性質のために、第二の最後の行からconsole.log("Outside allmealsloop length",allMeals.length);
は0
の長さを返しうまくとallMeals
Firebase DB console.log
戻り、すべてのデータが持つ配列であることを示し正しい長さ。
ejs
にこの配列をどのように渡すことができますか?allMeals
?