Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda