skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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);
    });
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search