Im trying to make query to my MongoDb and loop thru the response so I could have each user on its own object and push them to an array so I could send the response to client. But my array is empty if I use it outside of loop, if I try to use it inside of loop I get an error when trying to res.json() to client. I want to send finished array to client. How to do it so that the code runs the loop before the bottom console.log(s)
let placesArr = []
User.find({_id: getUser}).then( response => {
response[0].placeId.forEach(item => {
Place.find({placeId: item}).then(ress => {
const getUsersFromPlace = ress[0].users
getUsersFromPlace.forEach(element => {
if(getUser != element._id){
placesArr.push(element)
}
//here I can see the result -> console.log(placesArr)
})
})
})
//How to see finished array in here
//Array is empty here -> console.log(placesArr)
})
//How to see finished array in here
//Array is empty here -> console.log(placesArr)
});
