From 333b4b9fb38c87a7e70c75f8f32faba8cfb5bbec Mon Sep 17 00:00:00 2001 From: Tushar Bansal Date: Wed, 3 Apr 2024 11:36:46 +0530 Subject: [PATCH] fix: unique elements (#14) --- controllers/userOrderControl.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/controllers/userOrderControl.js b/controllers/userOrderControl.js index a38e6f6..51aaa69 100644 --- a/controllers/userOrderControl.js +++ b/controllers/userOrderControl.js @@ -100,8 +100,8 @@ exports.getRecommendation = async (req, res, next) => { const userID = req.params.user_id; let orders = await Order.find({ user_id: userID }) .sort({ createdAt: -1 }) - .limit(5); // Get the last 5 orders - let recommendedItems = []; + .limit(2); // Get the last 5 orders + let recommendedItemsSet = new Set(); // Fetch recommendations for default items const defaultItems = ["Samosa", "Pav Bhaji"]; @@ -118,7 +118,7 @@ exports.getRecommendation = async (req, res, next) => { for (let recipe of recommendedRecipes) { const dbItem = await MenuItem.findOne({ name: recipe }); if (dbItem) { - recommendedItems.push(dbItem); + recommendedItemsSet.add(JSON.stringify(dbItem)); } } } catch (e) { @@ -143,13 +143,15 @@ exports.getRecommendation = async (req, res, next) => { for (let recipe of recommendedRecipes) { const dbItem = await MenuItem.findOne({ name: recipe }); if (dbItem) { - recommendedItems.push(dbItem); + recommendedItemsSet.add(JSON.stringify(dbItem)); } } } catch (e) { console.log(e); } } + const recommendedItems = Array.from(recommendedItemsSet).map(item => JSON.parse(item)); + res.json({ recommendedItems }); } catch (error) {