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

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

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