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

111
Visualizações
How to send multiple query results from backend to frontend in node

I am making a simple check-in and check-out system for employees. In the main panel I want to show the number of hours worked per day and in another field the weekly total.

I can now show the hours per day in a table, the problem comes when I have to show the total since I make that query to the database with another query.

With the following code I perform the query and perform a render in the index view using the EJS templates.:

router.get('/', (req, res) => {
    const week = DateTime.now().weekNumber;
    conexion.query('SELECT * FROM assistance WHERE week = ?', week, (error, results) => {
        if(error){
            throw error;
        }else{
            res.render('index', {results: results});
        }
    })
});

With this code I perform the query of the total hours. but I don't know how to show it in the same index view since it is a separate query from the first one:

conexion.query('SELECT time_format(SUM(TIMEDIFF(a.exit, a.entry)), "%H:%i") AS hours from assistance a WHERE a.week = ?', week, (error, results) => {
        if(error){
            throw error;
        }else{
            return('index', {totalHours: results});
        }
    })

In this way I am trying to receive the information in the index view with EJS:

<div>
   <div>Total hours:<%= totalHours%></div>
</div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use try/catch async-await, then just do both queries.

router.get('/', async(req, res) => {
  const week = DateTime.now().weekNumber

  try {
    // query 1
    const totalHours = await conexion.query('SELECT time_format(SUM(TIMEDIFF(a.exit, a.entry)), "%H:%i") AS hours from assistance a WHERE a.week = ?', week)

    // query 2
    const results = await conexion.query('SELECT * FROM assistance WHERE week = ?', week)

    res.render('index', {
      totalHours,
      results
    })
  } catch {
    res.render('error-page')
  }
})

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