Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda