Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
Is it possible to send multiple data in one view on Node JS?

I am trying to pass several data to one view. I can send the products to it but I would like to send the categories as well. I want to send them separately and not with a join. I have been advised to put it after the "then" but I don't know how to do it.

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));
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Sure, use async/await to build up your data then pass it to the view.

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
  });
});

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda