我正在使用帶有胡子模板的 express 進行專案,我正在嘗試使用 res.locals 將物件傳遞給模板,但它不起作用,
這是代碼
exports.menu = function(req,res){
res.locals.title = "Menu";
res.locals.class_type ="main_nav";
MenuDB.GetAvailableLunch()
.then((list) => {
res.locals.lunch = list;
console.log('Promise Resolved');
})
.catch((err) =>{
console.log('Promise rejected', err);
})
res.render('menu');
}
我希望來自函式的串列成為我在胡子頁面上的午餐物件,有什么想法嗎?我知道我可以這樣做
.then((list) => {
res.render('menu', {
'lunch': list
});
但我正在嘗試這樣做,因為我將有另一個函式從資料庫回傳另一個串列,希望這足夠清楚。
uj5u.com熱心網友回復:
構建一個 Promise 鏈或一組獨立的 Promise,并且只有res.render()在一切都解決后才呼叫。
例如...
exports.menu = async (req, res) => {
const [lunch, another] = await Promise.all([
MenuDB.GetAvailableLunch(),
anotherFunction(),
]);
res.render("menu", {
title: "Menu",
class_type: "main_nav",
lunch,
another
});
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/466369.html
