Así que estoy tratando de encontrar un oponente para el usuario en función de sus trofeos, funciona bien cuando la condición no se ejecuta, pero cuando se ejecuta la condición, se ejecuta en un bucle infinito.
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 }```Si se trata de un bucle infinito, parece que su agregación sigue atrayendo al mismo usuario como 'user.id', que recursivamente llama a la función de emparejamiento una y otra vez.
Intentaría agregar una condición $not a su coincidencia que verifique el user.id. De esa forma, la agregación no devuelve el usuario original.