From 7d375309809fb3d8c18222c009c8df2c1bd4bd73 Mon Sep 17 00:00:00 2001 From: Matt Kirby <MattJKirby@outlook.com> Date: Sun, 16 Apr 2023 21:01:10 +0100 Subject: [PATCH] Seach returns no dupes now --- .../user-service/controllers/appController.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/backend-services/user-service/controllers/appController.js b/backend-services/user-service/controllers/appController.js index 40e1ca87..b5038175 100644 --- a/backend-services/user-service/controllers/appController.js +++ b/backend-services/user-service/controllers/appController.js @@ -245,9 +245,21 @@ export const GetUserList = async (req,res) => { const usersWithMatchingUsername = await UserModel.find({"username": {"$regex": `^${searchQuery}`}}, {password: 0, email: 0}) const usersWithMatchingName = await UserModel.find({$or: [{"firstName": {"$regex": `^${searchQuery}`,"$options": 'i'}}, {"lastName": {"$regex": `^${searchQuery}`,"$options": 'i'}}]}, {password: 0, email: 0}) - return res.status(201).send({ usersByUsername: usersWithMatchingUsername, usersByName: usersWithMatchingName}); + const result = getDistinctArray([...usersWithMatchingUsername, ...usersWithMatchingName]) + + return res.status(201).send({result: result}); } catch(error){ return res.status(401).send({ error: error.message }); } } +function getDistinctArray(arr) { + var dups = {}; + return arr.filter(function(el) { + var hash = el.valueOf(); + var isDup = dups[hash]; + dups[hash] = true; + return !isDup; + }); +} + -- GitLab