我有Express和MongoDB的Node应用程序。它具有3条路线:/ articles,/ directions和/ research。所有这3条路线都会呈现db中不同集合的标题,当我单击title时,它将导航至其余数据,并且路径如下所示:http:// localhost:3000 / articles / 59df896b7f13f25b9009c42e。当我尝试通过链接从该路由导航到/ directions时,它的工作原理和路径如下所示:http:// localhost:3000 / directions应该是正确的。但是,当我尝试导航(从此处http:// localhost:3000 / articles / 59df896b7f13f25b9009c42e)导航到/ research时,路径看起来像这样http:// localhost:3000 / articles / research并引发错误。正确的路径应为:http:// localhost:3000 / research。仅当我尝试导航到/ research时,它才起作用。所有3条路由都使用相同的逻辑和代码。我只是用说明等代替文章。

所以,我的问题是为什么应用程序导航到错误的路径?

代码片段-app / routes / directions.js,app / routes / articles.js和app / routes / researchers.js(我收到TypeError:无法读取//中的未定义属性'author',但只有一篇文章,但只有在导航时到/ research,如前所述。如果我从/ direction做相同的操作,则会收到相同的错误,但如果我尝试从/ research导航到任何路由,则效果很好):

// get one direction
router.get('/:id', (req, res) =>
    Direction.findById(req.params.id, (err, direction) =>
        User.findById(direction.author, (err, user) =>
            res.render('direction', {direction, author: user.name}))))

// get one article
router.get('/:id', (req, res) =>
    Article.findById(req.params.id, (err, article) =>
        User.findById(article.author, (err, user) =>
            res.render('article', {article, author: user.name}))))

// get one researcher
router.get('/:id', (req, res) =>
    Researcher.findById(req.params.id, (err, researcher) =>
        User.findById(researcher.author, (err, user) =>
            res.render('researcher', {researcher, author: user.name}))))


编辑。添加了其他代码片段。

来自app / app.js的代码片段

// article route
app.get('/news', (req, res) =>
    Article.find({}, (err, articles) => {
        if (err) {
            console.log(err)
        } else {
            res.render('news', {title: 'articles', articles})
        }
    }))

// direction route
app.get('/direct', (req, res) =>
    Direction.find({}, (err, directions) => {
        if (err) {
            console.log(err)
        } else {
            res.render('direct', {title: 'directions', directions})
        }
    }))

// researcher route
app.get('/research', (req, res) =>
    Researcher.find({}, (err, researchers) => {
        if (err) {
            console.log(err)
        } else {
            res.render('research', {title: 'researchers', researchers})
        }
    }))


app / views / research.pug

extends layout

block content
  h1.page-header #{title}
  ul.list-group
    each researcher, i in researchers
      li.list-group-item
        a.newsLinks(href='/researchers/' + researcher._id)= researcher.title


app / views / article.pug

extends layout

block content
  h1.page-header= article.title
  h5 Written by #{author}
  p= article.body
  hr
  if user
    if user.id ==article.author
      a.btn.btn-default(href='/articles/edit/' + article._id)
        span.glyphicon.glyphicon-edit
        |  Edit
      a.btn.btn-danger.delete-article(href='#' data-id=article._id)
        span.glyphicon.glyphicon-remove
        |  Delete


app / views / layout.pug

            li
              a(href='/news')
                | News
            if user
              li
                a(href='/articles/add')
                  span.glyphicon.glyphicon-plus
                  |  Add Article
            li
              a(href='/direct')
                | Direction
            if user
              li
                a(href='/directions/add')
                  span.glyphicon.glyphicon-plus
                  |  Add Direction
            li
              a(href='research')
                | Researchers
            if user
              li
                a(href='/researchers/add')
                  span.glyphicon.glyphicon-plus
                  |  Add Researcher

最佳答案

您的hrefs似乎有问题。

li
              a(href='research')
                | Researchers


应该

li
              a(href='/research')
                | Researchers


相对于您的根文件夹。

关于javascript - 在特定路线之间移动时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46714861/

10-09 16:52