From 5b2aef1e496688b01f9d0b736251d56e84156006 Mon Sep 17 00:00:00 2001 From: Franceska Date: Tue, 7 Jan 2025 22:36:21 -0500 Subject: [PATCH] "completed search controller and router, need to test it with front end #15" --- server/controllers/company.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/server/controllers/company.js b/server/controllers/company.js index a87ea24..b90b683 100644 --- a/server/controllers/company.js +++ b/server/controllers/company.js @@ -92,10 +92,32 @@ const companyController = { next(error) } }, - searchCompany: async (req, res, next ) => { - let companies = await Company.aggregate([ - - ]) + searchCompany: async (req, res, next) => { + try { + let companies = await Company.aggregate([ + { + $search: { + index: "kinfolk-search", + text: { + query: req.body.search, + path: { + wildcard: "*" + } + } + } + } + ]); + // if (companies.length === 1) { + // // If exactly one company matches, return it + // res.json({ redirect: true, company: companies[0] }); + // } else { + // // Otherwise, return the list of companies + // res.json({ redirect: false, companies }); + // } + res.json(companies); // Send the companies data as JSON response + } catch (error) { + next(error); // Handle errors appropriately + } } }