From 30ea4d671e30f44c9aebc8501d3bb228e1a6b2e2 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sun, 3 Oct 2021 19:21:47 +0200 Subject: [PATCH] You can now sort and order the searches. --- api/players/players.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/api/players/players.js b/api/players/players.js index 917093c..0282867 100644 --- a/api/players/players.js +++ b/api/players/players.js @@ -47,9 +47,12 @@ playerApi.get('/search', async (req, res) => { }) const name = req.query.name - const order = req.query.sortBy === "asc" || req.query.sortBy === "asc" ? req.query.sortBy : "asc" + const sort = req.query.sort ?? 'name' + const order = req.query.order === "desc" ? -1 : 1 + const page = req.query.page ?? 1 - const players = await Player.find({ name: { $regex: name, $options: 'i' }}) + + const players = await Player.find({ name: { $regex: name, $options: 'i' }}).sort([[sort, order]]).limit(20).skip((page - 1) * 20) if (!players) return res.json({