So I have the following piece of code in javascript
async function getFoodData (req, res) {
const ids = req.body.data;
const food = [];
ids.map((id) => {
Food.findOne({ _id: id },(err, result) => {
if(result){
food.push(result)
}
})
})
return res.status(200).json({ Food: food })
}
The ids are a bunch of Mongo Id's received in the body This function looks for data and on returning results it adds it into the food array. However, the return statement runs first and I get an empty array. I think my problem will be solved by using Promises but I am new to the concept and hence can someone help me with it.