Fixed broken recent matches endpoint.
This commit is contained in:
parent
8a7fbd074b
commit
b2c4178482
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user