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

81
Vistas
Send response after multiple asynchronous queries with mongoose and express

I try to make a list with products that I send to the client, but the res.send gets executed before the loop has finished. Do you have any suggestions? Async/await doesn't seem to work.

Here is my code:

const Outfits = require("../models/Outfits");
const Products = require("../models/Products");

module.exports = (req, res) => {
    Outfits.find({ outfitId: req.params.id }, async (error1, result) => {
        if (error1) {
            res.status(400).json({
                status: "fail",
            });
        } else {
            let productIds = result[0].productIds;
            let productList = [];
            for (let i = 0; i < productIds.length; i++) {
                await Products.find({ id: productIds[i] })
                    .then((product) => {
                        console.log(product);
                        productList.push(product);
                    })
                    .catch((error2) => {
                        res.status(400).json({
                            status: "fail",
                        });
                    });
            }
            console.log(productList);
            res.status(200).json({
                status: "success",
                body: productList,
            });
        }
    });
};

Thank you very much!

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

0

In order to have multiple asynchronous tasks executed at once, a cleaner way is to use a Promise.all and pass an array of your asynchronous tasks. Like so:

const Outfits = require("../models/Outfits");
const Products = require("../models/Products");

module.exports = (req, res) => {
    Outfits.find({ outfitId: req.params.id }, async (error1, result) => {
        if (error1) {
            res.status(400).json({
                status: "fail",
            });
        } else {
            let productIds = result[0].productIds;
            
            //
            const productList = await Promise.all(
                 productIds.map((id) => Products.findById(id))
            );
           
            console.log(productList);
            res.status(200).json({
                status: "success",
                body: productList,
            });
        }
    });
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

Do you have any suggestions? Async/await doesn't seem to work

The async/await in your code does not work it is because of the function mongoose gives you.

  1. callback style

    // finds the outfit and passes the outfit to the callback

    Outfits.find({ outfitId: req.params.id }, async (error1, outfit) => {

    // your other logic

    })

  2. async/await style

    // finds the outfit and returns

    const outfit = await Outfits.find({outfitId: req.params.id }).exec();

Assuming you retrieve array of product id this way

let productIds = result[0].productIds;

const products = await Promise.all(

productIds.map((productId)=>{

   return Products.findById(productId)

}

)

This should retrieve you the list of products

This is my first time answering a question hence the try hard answer haha :).

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to call await on the Products.find function like this

for (let i = 0; i < productIds.length; i++) {
  try {
    const product = await Products.find({ id: productIds[i] }).exec()
    console.log(product);
    productList.push(product);
  } catch (error) {
    res.status(400).json({
      status: "fail",
    });
  }
}
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