是否可以在 Node JS 的一个视图中发送多个数据?
Is it possible to send multiple data in one view on Node JS?
我正在尝试将多个数据传递到一个视图。我可以将产品发送给它,但我也想发送类别。我想单独发送它们而不是加入。有人建议我把它放在“然后”之后,但我不知道该怎么做。
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));
});
当然可以,使用 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
});
});
我正在尝试将多个数据传递到一个视图。我可以将产品发送给它,但我也想发送类别。我想单独发送它们而不是加入。有人建议我把它放在“然后”之后,但我不知道该怎么做。
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));
});
当然可以,使用 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
});
});