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

83
Vistas
Trying to make a matchmaking function

So i am trying to find an opponent for user based on his trophies, it works fine when if condition isnt run but when if condition runs, it runs infinte loop

const UserProfile = require("../schemas/userProfile")
async function matchmake(user, message) {

   let UserProfileDetails = await UserProfile.findOne({ userID: user.id });
   let userTrophies = UserProfileDetails.trophies;

   let userMatched = await UserProfile.aggregate([
      { $match: { trophies: { $gte: userTrophies - 10, $lte: userTrophies + 10 } } },
      { $sample: { size: 1 } }
   ]);

   let otherUserID = userMatched[0].userID;
   console.log("userID -"+otherUserID);
   if (otherUserID === user.id) {
      otherUserID = await matchmake(user, message);
   }
   return otherUserID;
}

module.exports = { matchmake }```


about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If it's infinite looping, it seems that your aggregation keeps pulling the same user as 'user.id', which recursively calls the matchmake function again over and over.

I would try to add a $not condition to your match that checks for the user.id. That way, the aggregation doesn't return the original user.

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