Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

560
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!