I’m trying to make a website that show you all the restaurants in the country with all th different cities. I add a route for restaurants that use params to redirect you to the restaurant page.
JavaScript
x
16
16
1
router.get('/:name',checkUser, async (req, res) => {
2
3
try {
4
5
const wila = await wilaya.findOne({nom : req.params.name})
6
const re = await restaurant.find({ville : wila.nom})
7
8
res.render('html/villeDetails', {
9
wilay: wila,
10
title : wila.nom,css : "villeDetails",
11
resto : re
12
})
13
} catch {
14
res.redirect('/')
15
}
16
})
And another route that also use params to take you to the city details page
JavaScript
1
19
19
1
router.get('/:id',checkUser, async (req, res) => {
2
3
try {
4
console.log('here')
5
const resto = await restaurant.findById(req.params.id)
6
comment.find({resId : req.params.id})
7
.then((result) => {
8
res.render('html/restaurantDetails', {
9
res: resto,
10
title : resto.nom,
11
css : "restaurantDetails",
12
comm : result
13
14
})
15
})
16
} catch {
17
res.redirect('/')
18
}
19
})
The problem is that only the restaurant route is working and when i delete the restaurant route, the city route start working . i don’t know why this is happening .
Advertisement
Answer
/:name
and /:id
are both on the same route(/
). I have noticed that when this happens only the route that is seen first (/:name
) would be recognised. You can try changing the route for one of them like /:name
for the restaurant route and /restaurant/:id
for the city route.