It's a React, Node, MongoDB and Express project. I am trying to get multiple fields search on my Find Jobs page. The route looks like this :
<Route path="/findjobs/:keyword?/:location?/:category?/:page?">
<FindJobs />
</Route>
In my redux code, I get the different parameters and I send them to the back end.
const { data } = await axios.get(`/api/jobs?keyword=${keyword}&location=${location}&category=${category}`);
With the only field "keyword" everything is fine, I manage to display the results.
const getJobs = asyncHandler(async(req, res) => {
const keyword = req.query.keyword ? {
title: {
$regex: req.query.keyword,
$options: 'i'
}
} : {}
const jobs = await Job.find({ ...keyword });
res.json(jobs);
})
I would like to know how to do the same thing but with all the fields at once or with only the second or the third field. I also noticed that with React, when I pass the second field while the first is empty, it creates a link like this "localhost:3000/findjobs//newyork". Am i doing something wrong with the optional parameters?