I am a newbie in JavaScript and I am a bit stuck with the async way JS executes code...
Here is my code :
var marketPlaces = []
body.rows.forEach(function(doc) {
marketPlacesDB.get(doc.id, function(err, body){
if (err) {
callback(err, null)
} else {
console.log("Ajout d'une marketplace")
marketPlaces.push({id: body._id, name: body.name})
}
})
})
console.log("Retour des résultats")
callback(null, { marketPlaces: marketPlaces })
body.rows is an array containing ids of objects I would like to return in the marketPlaces array. For each element, I need to make a new request to the database to get the details of the objects (here only the "name").
The result is an empty array because the foreach loop ends before the callbacks of the get function return.
I can't figure out how to make this "synchronous".
Thanks for your answers. Philippe.