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

558
Vistas
Saving array of children of a Mongoose Schema and then adding returned IDs to parent

i'm trying to loop over an array of objects, saving them to MongoDB and then add the returned ObjectIds to a parent Schema which then is also saved. I'm at a loss here.

Everything gets saved correctly but the Recipe (parent) apparently is saved before I get the returned ObjectIds of the Tags (children). I feel like I've used the async and await keywords a bit to often.

Can someone help? Code simplified, but I can post more if needed.

Parent Schema:

const recipe = new mongoose.Schema(
    {
        name: String,
        ingredients: [
            {
                type: mongoose.SchemaTypes.ObjectId,
                ref: "Ingredient",
            },
        ],
    }
);

Child Schema:

const ingredientSchema = new mongoose.Schema({
    value:  String,
    label: String,
});

Payload:

{
    name: "Rezept",
    ingredients: [
        {
            label: "zutat",
            value: "Zutat",
        },
        {
            label: "schokolade",
            value: "Schokolade",
        },
    ],
};

My router:

recipesRouter.post("/", async (req, res) => {
    const { body } = req;
    const saveIngredients = async () => {
        let ingredientIDs = [];
        body.ingredients.map(async (ingredient) => {
            const i = new Ingredient({
                value: ingredient.value,
                label: ingredient.label,
            });

            const savedIngredient = await i.save();
            ingredientIDs.push(savedIngredient._id);
        });
        return ingredientIDs;
    };    

    const recipe = new Recipe({
        name: body.name,        
        ingredients: (await saveIngredients()) || [],
       
    });
    const savedRecipe = await recipe.save();
    res.status(201).json(savedRecipe);
});

Returned recipe:

savedRecipe: {
    name: 'asd',
    ingredients: [],
    _id: new ObjectId("62782b45a431e6efb7b8b1a7"),
}

As I said, both ingredients individually and the recipe is saved to the MongoDB after this but not the ingredient IDs in the recipe. The returned recipe has an empty array in ingredients. I guess the recipe is saved too soon before MongoDB can return ObjectIds for the ingredients.

Thanks for any help.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

First of all, your post method is an async, so everything inside it is wrapped in a resolved promise automatically.

Do you really need to make your saveIngredients as an async? IMHO, it's better to let the saveIngredients not be in another async.

And then we can remove the empty list, and just wait for the saveIngredients() finish first.

 const recipe = new Recipe({
        name: body.name,        
        ingredients: await saveIngredients(),      
 });

Your guess is correct, the Recipe was saved first because all the conditions are fulfilled because it doesn't need to wait for the saveIngredients since you provided a [] as the default value. And your saveIngredients is run in parallel.

over 4 years ago · Santiago Trujillo Denunciar

0

I got it smh. Turns out async in a .map or .foreach doesn't go well. I turned it into a simple for loop. It's still bloated/lot of steps imo but it works!

recipesRouter.post("/", async (req, res) => {
    const { body } = req;

    const saveIngredients = async () => {
        let ingredientIDs = [];
        for (let i = 0; i < body.ingredients.length; i++) {
            const el = body.ingredients[i];
            const ing = new Ingredient({
                value: el.value,
                label: el.label,
            });
            const savedIngredient = await ing.save();
            ingredientIDs.push(savedIngredient._id);
        }
        return ingredientIDs;
    };

    const ingredientIDs = await saveIngredients();

    const recipe = new Recipe({
        name: body.name,
        ingredients: ingredientIDs,
    });

    const savedRecipe = await recipe.save();
    res.status(201).json(savedRecipe);
});
over 4 years ago · Santiago Trujillo 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