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

176
Views
save results of a promise to a variable in controller?

I am stuck on promise resolution, can anyone explain to me how to store the promise result in a variable? I am working with mongoDB and I want to get some data from it. here is my example.

const checkRelated = new Promise((resolve, reject) => {
     return RelatedProducts.find({
        parent_id: { $in: product_id.split(',') },
      })
        .then((res) => reject(res))
        .catch(err => reject(err))

    })

after getting the data I want to do some validation. For example.

if(checkRelated.length > 0) {
do smth....
}

but to my surprise I get "promise {pending}"

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You're overcomplicating Mongoose, if you want to do an async function you can but I've never seen a new Promise being used with Mongoose. If you want to create an async method with mongoose do the following

exports.getRelatedProducts = async (req, res) => {
  try {
    const checkRelated = await RelatedProducts.find({parent_id: { 
  $in: product_id.split(',') }})

    if (checkRelated.length > 0){
      do smth...
    }
  } catch (error) {
      reject(error)
  }
}

but keep in mind you must create the async method first. then you can use await.

about 4 years ago · Santiago Trujillo Report
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!