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

112
Vistas
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 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