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

142
Vistas
How can I solve the asynchronous problem in my code?

Even though the updating is happening successfully in the DB (collection()....) - the batchIdsAdded data that's supposed to be pushed into packIdsOwned and packsOwnedMetadata arrays in the for loop is displaying as null in the response.

I know there's an asynchronous problem in my code but I'm not sure how to tackle it in my code even after taking a look at this popular SO post revolving around this and trying out some of the suggestions there. Unfortunately, I've hit a wall and I'm very stuck.

How can I make it so that all the batchIdsAdded data is pushed to the arrays correctly?

let packsOwned = result.packsOwned;
let packIdsOwned = [];
let packsOwnedMetadata = [];

let batchIdsAdded = {};

result.packsOwned.push(...batchPackIdInput.map(item => (batchIdsAdded = {"PackId" : item}, collection.updateOne(query, {$push: {"packsOwned" : { $each: [batchIdsAdded]}}}, result['userName']))));  

// Data to be displayed in the response
for(let i = 0; i < packsOwned.length; i++) {
   packIdsOwned.push(packsOwned[i].PackId);
   packsOwnedMetadata.push({
      PackID : packsOwned[i].PackId
   });
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I'm not seeing where the async is happening, my best guess is collection.updateOne. If that's the case, then you'll need to Promise.all() the map since at the time of calling it's still a bunch of promises. Unless result is also in an async context tho, it won't matter since everything will be off the stack by the time it goes to push. You'll need to keep everything in an async context after you start one.

async function updateResult() {
  let batchIdsAdded = {};
  return await result.packsOwned.push(Promise.all(...batchPackIdInput.map(
    item => (
      batchIdsAdded = {"PackId" : item},
      collection.updateOne(
        query,
        {$push: {"packsOwned" : { $each: [batchIdsAdded]}}},
        result['userName']
       )
      )
    )
  ));  
}

async function display() {
  let packsOwned = await updateResult().packsOwned;
  let packIdsOwned = [];
  let packsOwnedMetadata = [];
  return packsOwned.forEach(({PackId}) => {
    packIdsOwned.push(PackId)
    packsOwnedMetadata.push({PackID: PackId})
  })
}

about 4 years ago · Juan Pablo Isaza 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