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

300
Vistas
How do I execute a code only once all the queries inside a loop are done? (mongoose, express)

here is my router:

router.get('/questions/best', function(req,res,next){
  Question.aggregate([
    // Protect against missing fields with $ifNull                                                         
    // Protect against division by zero dislikes using max[1,size]                                         
    {$project: {ratio: {$divide:[
    {$arrayElemAt:[{$ifNull:["$likes",[0]]}, 0]},
        {$max:[1, {$arrayElemAt:[{$ifNull:["$dislikes",[0]]}, 0]}]}
        ]}
    }},
    {$sort: {ratio:-1}},
    {$limit: 15}
  ]).exec(function(err,results){
    if(err) {return next(err)}
    top15 = []
    for(let i = 0; i < results.length; i++){
      Question.findOne({_id: results[i]._id}).exec(function(err,result){
        if(err){return next(err)}
        top15.push(result);
      })
    }
    res.render('content', { whichOne: 5, user: req.user, questions: top15 });
  });
});

don't worry about the filtering stuff, it works good, the only problem is that the template at the end of the router get rendered automatically right after the queries start, not allowing them time to push the results into an array that gets rendered into the template. I tried using set Timeout function, but the processing time changes drastically and it is impossible to find an exact time. I think I need to use async functions? but promises do not work for some reason?

can anybody make sure the template get rendered only after all the queries return and put their values in the array? Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

From reading mongoose documentation, using promises makes this trivial

using Promises

router.get('/questions/best', (req,res,next) => {
    Question.aggregate([
        // Protect against missing fields with $ifNull                                                         
        // Protect against division by zero dislikes using max[1,size]                                         
        {$project: {ratio: {$divide:[
        {$arrayElemAt:[{$ifNull:["$likes",[0]]}, 0]},
            {$max:[1, {$arrayElemAt:[{$ifNull:["$dislikes",[0]]}, 0]}]}
            ]}
        }},
        {$sort: {ratio:-1}},
        {$limit: 15}
    ]).exec()
    .then(results => Promise.all(results.map(({_id}) => Question.findOne({_id}).exec())))
    .then(questions => res.render('content', { whichOne: 5, user: req.user, questions}))
    .catch(next);
});

or, using async/await

router.get('/questions/best', async (req,res,next) => {
    try {
        const results = await Question.aggregate([
            // Protect against missing fields with $ifNull                                                         
            // Protect against division by zero dislikes using max[1,size]                                         
            {$project: {ratio: {$divide:[
            {$arrayElemAt:[{$ifNull:["$likes",[0]]}, 0]},
                {$max:[1, {$arrayElemAt:[{$ifNull:["$dislikes",[0]]}, 0]}]}
                ]}
            }},
            {$sort: {ratio:-1}},
            {$limit: 15}
        ]).exec();
        const questions = await Promise.all(results.map(({_id}) => Question.findOne({_id}).exec()));
        res.render('content', { whichOne: 5, user: req.user, questions});
    } catch(err) {
        next(err);
    }
});
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