title: How to make async callback in node and make sure HTML form data is available for saving to db
body: <p>How can I make callback and variables so that I can save data to my database?</p>
  
  <p>I have node app.post like below. I need to convert form data to mongoose model. The data what I'm getting is in two arrays and only data which has amount larger than 0 is searched and saved, but this don't work and I can't get around it.</p>
  
  <p>I think that looping and searching for ingredients should be a function and Meal.save should be in callback but I don't know how. I've been banging my head into this problem for weeks and I have googled, but this just does not open to me. Please help me with this.</p>
  
  <p>Thanks in advance!</p>
  
  <p>app.js</p>
  
  <pre><code>app.post("/diary/:id/dayshow", function(req, res) {
  
  // Get the day I want to save my meal data
    Day.findById(req.params.id, function(err, day) {
      if (err) {
        console.log(err);
        res.redirect("/diary");
      } else {
  
  // This is the format I have in the mongoose model
  // and I need to convert HTML form data to this
  
          var singleMeal =
            {
            //       title: dinner
            //       ingredients: [
            //                      {
            //                        ingredient:{},
            //                        amount: Number
            //                      }
            //                    ]
            }
          var singleMealTempArr = [];
          var singleIngredientObj = {};
          var amount;
          singleMeal.title = req.body.title;
  
  // Looping through the HTML form data and converting it to mongoose model
  // I want to loop data and search mongo first and after that I need to save 
  // the data to mongo which happens in Meal.save()
  
          for (var i = 0; i &lt; req.body.amount.length; i++) {
            var _id = req.body.id[i];
            amount = Number(req.body.amount[i]);
            if (amount !== 0) {
              singleIngredientObj.amount = amount;
  
  // Searching ingredient from database with ID what is from the HTML form
  
              Ingredient.findById({_id}, function(err, foundIngredientData){
                console.log(foundIngredientData + "&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; ingredientData");
                singleIngredientObj.ingredient = foundIngredientData;
                singleMealTempArr.push(singleIngredientObj);
                console.log(JSON.stringify(singleMealTempArr) + "&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; JSON.stringify(singleMealTempArr)");
              });
            }
          }
          singleMeal.ingredients = singleMealTempArr;
  
   // Now I should have data in singleMeal variable, but it has only title and 
   // empty array of ingredients
          console.log("JSON.stringify(singleMeal) &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;" + JSON.stringify(singleMeal) + "&lt;&lt;&lt;&lt;&lt; JSON.stringify(singleMeal)");
        }
  
         Meal.create(singleMeal, function(err, singleMealObject) {
          if (err) {
            console.log(err);
          } else {
        console.log("--- You hit the Meal.create + Save -----" + "singleMealObject: " + singleMealObject);
          //  day.meals.push(singleMealObject);  &lt;-- These two are commented because previous steps dont work and singleMealObject has only title and empty array of ingredients
          //  day.save();
  
            res.redirect("/diary/" + day._id + "/dayshow");
          }
        });
    });
  });
  </code></pre>
  
  <p>console.logs what I get from the above is here:</p>
  
  <pre><code>JSON.stringify(singleMeal) &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;{"title":"aa","ingredients":[]}  &lt;&lt;&lt;&lt;&lt; JSON.stringify(singleMeal)
  
  { _id: 597c11c04a7bce08cdcdef41,
    name: 'oatmeal',
    kcal: 100,
    protein: 2,
    carb: 50,
    fat: 1,
    addinfo: '',
    __v: 0 }  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; ingredientData
  
  [{"amount":3,"ingredient":{"_id":"597c11c04a7bce08cdcdef41","name":"oatmeal","kcal":100,"protein":2,"carb":50,"fat":1,"addinfo":"","__v":0}}]  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; JSON.stringify(singleMealTempArr)
  
  { _id: 597c11c04a7bce08cdcdef41,
    name: 'oatmeal',
    kcal: 100,
    protein: 2,
    carb: 50,
    fat: 1,
    addinfo: '',
    __v: 0 } &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; ingredientData
  
  [{"amount":3,"ingredient":{"_id":"597c11c04a7bce08cdcdef41","name":"oatmeal","kcal":100,"protein":2,"carb":50,"fat":1,"addinfo":"","__v":0}},{"amount":3,"ingredient":{"_id":"597c11c04a7bce08cdcdef41","name":"oatmeal","kcal":100,"protein":2,"carb":50,"fat":1,"addinfo":"","__v":0}}]&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; JSON.stringify(singleMealTempArr)
  
  --- You hit the Meal.create + Save -----singleMealObject: { __v: 0,
    title: 'aa',
    _id: 597c11cb4a7bce08cdcdef61,
    ingredients: [] }
  </code></pre>