两条路线使用参数,但只有一个正在工作

发布于 2025-01-29 10:49:16 字数 1209 浏览 2 评论 0原文

我正在尝试建立一个网站,向您展示该国所有不同城市的所有餐馆。 我为使用参数将您重定向到餐厅页面的餐馆添加了一条路线。

router.get('/:name',checkUser, async (req, res) => {

    try {
        
        const wila = await wilaya.findOne({nom : req.params.name})
        const re = await restaurant.find({ville : wila.nom})
        
        res.render('html/villeDetails', {
        wilay: wila,
        title : wila.nom,css : "villeDetails",
        resto : re
        })
    } catch {
        res.redirect('/')
    }
    })

还有另一条也使用参数将您带到城市详细信息页面的路线

router.get('/:id',checkUser, async (req, res) => {
  
    try {
      console.log('here')
      const resto = await restaurant.findById(req.params.id)
      comment.find({resId : req.params.id})
      .then((result) => {
        res.render('html/restaurantDetails', {
        res: resto,
        title : resto.nom,
        css : "restaurantDetails",
        comm : result
        
      })
      })
    } catch {
      res.redirect('/')
    }
  })

问题在于,只有餐厅路线才能工作,当我删除餐厅路线时,城市路线开始工作。 我不知道为什么会发生这种情况。

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.

router.get('/:name',checkUser, async (req, res) => {

    try {
        
        const wila = await wilaya.findOne({nom : req.params.name})
        const re = await restaurant.find({ville : wila.nom})
        
        res.render('html/villeDetails', {
        wilay: wila,
        title : wila.nom,css : "villeDetails",
        resto : re
        })
    } catch {
        res.redirect('/')
    }
    })

And another route that also use params to take you to the city details page

router.get('/:id',checkUser, async (req, res) => {
  
    try {
      console.log('here')
      const resto = await restaurant.findById(req.params.id)
      comment.find({resId : req.params.id})
      .then((result) => {
        res.render('html/restaurantDetails', {
        res: resto,
        title : resto.nom,
        css : "restaurantDetails",
        comm : result
        
      })
      })
    } catch {
      res.redirect('/')
    }
  })

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 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟月 2025-02-05 10:49:16

/:名称/:ID都在同一路由(/)上。我注意到,只有首先看到的路由(/:name)才能识别。您可以尝试更改其中之一的路线,例如/:餐厅路线的名称/餐厅/:ID城市路线的ID 。

/: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.

泪是无色的血 2025-02-05 10:49:16

是的。当您使用名为path参数(例如/:ID/:name/:post_id您在做什么ID告诉您的Express Router with you wit y y y y y y y hou将在URI的那一部分中传递“某物”,并且您希望将其存储在带有该名称的变量中。它不知道您是否正在传递ID,名称或其他内容。为此使用嵌套路线。我建议您检查本指南

Yes. When you use named path parameters like /:id, /:name or /:post_id what you are doing id telling the express router that you are going to pass "something" in that segment of the URI and that you'd like for that be stored in a variable with that name. It doesn't know whether you are passing an id or a name or something else. Use nested routes for this purpose. I'd recommend you check this guide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文