Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda