So I have Been Writing this function all night, its goal is to find and sort data from two tables and then return an array of objects containing my needed data But the array keep showing empty
router.post('/find/', (req,res,next)=>{
try {
// Return an array of Hotels in the Region
hotels.find({region:"wuse"}).then(hotel=>{
const finalArr = []
for(var i = 0; i< hotel.length;i++){
rooms.find({hotel:hotel[i]._id,price:{$lt : 29000}}).then(rooms=>{
for(var j = 0; j <rooms.length; j++ ){
// Push to the arr
finalArr.push(rooms[j])
console.log(rooms[j]);// Its Works
}
}).catch(err =>{
console.log(err);
})
}
console.log(finalArr);// And This Return [] an Empty Array
res.end()
}).catch(err =>{
console.log(err);
})
} catch (error) {
console.log(error);
}
})
I have used Async/await, append(), forEach() and Promise.all()
any solution would.