0

I have a function that accesses a database to determine total emissions for each ingredients. This value is then saved into an object and into the database. However, the object is being saved before the emission totals can be calculated.

var emission_total = 0;

      async function getEmissionTotal() {
        for (var x in ingredients) {
          Emission.findOne({food_name:ingredients[x]}).then(result => {
            console.log(result);
            if (result == null) {
              emission_total += 0;
              console.log(emission_total);
            }
            else {
              emission_total += 0.7;
              console.log(emission_total);
            }
          })
          .catch(err => console.log(err));
        }
        return;
      }
      
      async function next() {
        await getEmissionTotal();
      
        const date = req.body.date; 
        const description = req.body.description; 
        const food_list = ingredients;
        const photo = photo_link;
        const nutrients = {
          'calories': req.body.calories,
          'fat': req.body.fat,
          'sugar': req.body.sugar,
          'carbs': req.body.carbs,
          'cholesterol': req.body.cholesterol,
          'protein': req.body.protein,
          'emissions': emission_total
        }
        
        console.log(nutrients);
          

        const newNutrition = new Nutrition({
          date,
          description,
          photo,
          nutrients,
          food_list
        });

        console.log(newNutrition)

        newNutrition.save()
          .then(() => res.json('Nutrition added!'))
          .catch(err => res.status(400).json('Error: ' + err));
     
      }
      next();




In essence, the next() function should be executed after the async function getEmissionTotal()

2
  • 1
    getEmissionTotal() doesn't return anything to wait for Commented Sep 2, 2020 at 0:40
  • bring the emission_total declaration into the getEmissionTotal() and then return emission_total. In the next() function, declare another variable const emissionTotal = await getEmissionTotal() Commented Sep 2, 2020 at 0:45

1 Answer 1

2

emission_total need to be brought inside getEmissionTotal method. Also you are returning nothing and not waiting for the result.

async function getEmissionTotal() {
    let emission_total = 0;
    for (var x in ingredients) {
        try {
            const result = await Emission.findOne({food_name:ingredients[x]});
            console.log(result);
            if (result == null) {
                emission_total += 0;
                console.log(emission_total);
            } else {
                emission_total += 0.7;
                console.log(emission_total);
            }
      } catch(err) {
          console.log(err);
      }  
   }
   return emission_total;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.