0

I am doing an external API call to get some data and i want to save it to my mongodb.

var today = moment().format('dddd');
var menu = new Menu();
menu = await restaurant.days.forEach(async day => {
    if(day.dayOfWeek.toLowerCase() == today.toLowerCase()){
        return await day.meals.forEach(async meal =>{
            var menuMeal = new MenuMeal();
            const [price, description] = await Promise.all([getInfoFromSelector(meal.priceSelector, url), getInfoFromSelector(meal.mealSelector, url)]);
            menuMeal.price = price;
            menuMeal.description = description;
            menuMeal.type = meal.mealType;
            menu.menuMeals.push(menuMeal);
            return menu;
        })
    }
})
restaurant.menu = menu;
restaurant.save();

I don't think i have understood the async js methods properly. Can anyone point me in the right direction here so that i don't save underfined all the time.

Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78

0 Answers0