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

115
Visualizações
Meteor Collection Hooks - How to run the 'after insert' hook only once the previous 'after insert' has finished

Inserts are happening quicker than the after.insert function can process them and what I want to happen after the current document is inserted is dependent on what happened after the previous document was inserted.

I think the short question might be how do I make it synchronous?

FirstCollection.after.insert(function (userId, doc) {

  lastDoc = SecondCollection.findOne({}, { sort: { $natural: -1 } });

  if (!lastDoc) {
    SecondCollection.insert({
      startNumber: doc.txNumber,
      count: 1,
      duration: 264,
      lastTx: doc.txNumber,
      time: Date.now(),
      finishedAt: 0,
    });
    return;
  }

  if (lastDoc.finishedAt !== 0) {
    if (Date.now() - lastDoc.finishedAt < 60000) {
      return;
    }

    SecondCollection.insert({
      startNumber: doc.txNumber,
      count: 1,
      duration: 264,
      last: doc.txNumber,
      time: Date.now(),
      finishedAt: 0,
    });
    return;
  }

  SecondCollection.update(
    { _id: lastDoc._id },
    { $set: { lastTx: doc.txNumber }, $inc: { count: 1 } }
  );

  if (lastDoc.count === lastDoc.duration) {
    SecondCollection.update(
      { _id: lastDoc._id },
      { $set: { finishedAt: Date.now() } }
    );
  }
});
about 4 years ago · Juan Pablo Isaza
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