我正在嘗試將多個資料傳遞給一個視圖。我可以將產品發送給它,但我也想發送類別。我想單獨發送它們,而不是聯合發送。有人建議我把它放在“then”之后,但我不知道該怎么做。
router.get('/', function (req, res) {
models.produit.findAll(
{
include:
[
{
model: models.image, as: "images"
},
{
model: models.promotionproduit, as: "promotionproduits",
include:[{model: models.promotion, as: "Promotion"}]
}
]})
.then(data => {
res.render('home', {
data: data
});
}).catch(err => console.log(err));
});
uj5u.com熱心網友回復:
當然,使用 async/await 來構建您的資料,然后將其傳遞給視圖。
router.get('/', async(req, res) => {
// define your data var
const data = {
errors: {}
produits: [],
categories: []
}
// get produits
try {
data.produits = await models.produit.findAll({
include: [{
model: models.image,
as: "images"
},
{
model: models.promotionproduit,
as: "promotionproduits",
include: [{
model: models.promotion,
as: "Promotion"
}]
}
]
})
} catch {
data.errors.produits = 'échec du chargement des produits.'
}
// get categories
try {
// as above, do your model...
} catch {
data.errors.categories = 'échec du chargement des catégories.'
}
res.render('home', {
data
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/379599.html
標籤:javascript 节点.js 表达 sequelize.js
下一篇:React從類中獲取值
