I'm trying to show the data from DB in a table by list of cities. These cities are contained in a select option. Using form = get method I was able to get the value of the cities in the URL when I select a city then click the submit button inside my form. However, I think my router.get(/:city) does not read the URL with the city value on it for my table.ejs does not get rendered on the UI. How do I make my get method read my url with param on button click?
This is the screenshot of the URL on button click
This is my index.ejs file
<form method="GET">
<div class="col">
<div class="mb-3 row">
<label class="col-md-4 col-sm-4 col-lg-2 col-form-label fs-6">Category:</label>
<div class="col-md-4 col-sm-4 col-lg-7">
<select class="form-select form-select-sm mt-1" id="selectCity" name="city">
<option selected>Select a city...</option>
<option name= "Manila" value="Manila">Manila</option>
<option name= "Quezon City" value="Quezon City">Quezon City</option>
<option name= "Pasig" value="Pasig City">Pasig City</option>
</select>
</div>
<div class="col-md-4 col-sm-4 col-lg-3">
<button type="submit" class="btn btn-sm btn-outline-primary mt-1" id="getCity">Show</button>
</div>
</div>
</div>
</form>
This is my routes.js get method section.
//Homepage
router.get("/", function(req, res){
res.render('index')});
//Display data in table by city
router.get("/:city", function(req, res){
const city = req.params.city;
CityModel.find()
.then(result => {
res.render('table', {vaccines:result});
//console.log({vaccines:result});
})
.catch(err => {
res.status(500).send({message: "Cannot display data!"});
console.log(err);
})});
As you can see I was not able to use the city param yet for I want to make the unfiltered table with all the data appear first when I click the Show button. I was able to do this with the :id param but when I try with other fields in my DB it does not work.