Fixed broken recent matches endpoint.

This commit is contained in:
Harrison Deng 2022-04-05 16:19:05 -05:00
parent 8a7fbd074b
commit b2c4178482

View File

@ -48,12 +48,11 @@ MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
}
let recent = null;
if (user) {
await user.populate("participatingMatches").populate("participatingMatches.members.$");
recent = user.participatingMatches.slice(-limit);
recent = matchModel.find({ creator: user._id });
} else {
recent = await matchModel.find().where("publicity").gte(2).limit(limit).sort({ createDate: -1 }).populate("members.$");
recent = matchModel.find().where("publicity").gte(2);
}
await recent; // Populates all references.
recent = await recent.sort({ createDate: -1 }).limit(limit).populate(["sport", "participants"]);
res.status(200).send({ recent: recent });
} catch (error) {
console.error(error);
@ -76,6 +75,10 @@ MatchController.post("/", needDatabase, requireAuthenticated, async (req, res) =
sport: await sportModel.findByName(req.body.sport),
participants: [user._id]
});
if (!match.sport) {
res.status(400).send("Invalid sport name provided.");
return;
}
await match.save();
user.createdMatches.push(match._id);
user.participatingMatches.push(match._id);