Currently, I am working on a matchmaking system to find suitable opponents based on a user's trophies. Everything seems to be working fine until the if condition is triggered, which results in an infinite 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 }