Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
JavaScript Array

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!