Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在特定路由之间移动时出错_Javascript_Node.js_Mongodb_Express - Fatal编程技术网

Javascript 在特定路由之间移动时出错

Javascript 在特定路由之间移动时出错,javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,我有带Express和MongoDB的节点应用程序。它有三条路线:文章、方向和研究。所有3条路由都在db中呈现来自不同集合的标题,当我单击标题时,它将导航到其余数据,路径如下所示:http://localhost:3000/articles/59df896b7f13f25b9009c42e. 当我尝试通过链接从该路线导航到/方向时,它会工作,路径如下所示:http://localhost:3000/directions 应该如此。但是当我尝试(从这里)导航时http://localhost:30

我有带Express和MongoDB的节点应用程序。它有三条路线:文章、方向和研究。所有3条路由都在db中呈现来自不同集合的标题,当我单击标题时,它将导航到其余数据,路径如下所示:http://localhost:3000/articles/59df896b7f13f25b9009c42e. 当我尝试通过链接从该路线导航到/方向时,它会工作,路径如下所示:http://localhost:3000/directions 应该如此。但是当我尝试(从这里)导航时http://localhost:3000/articles/59df896b7f13f25b9009c42e)要进行研究,路径如下所示http://localhost:3000/articles/research 并抛出错误。正确的路径应该是:http://localhost:3000/research. 只有当我尝试导航到/研究时,它才起作用。所有3条路由使用相同的逻辑和代码。我只是用说明等代替文章

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

代码片段-app/routes/directions.js、app/routes/articles.js和app/routes/searchers.js(我得到了TypeError:无法读取//get one article中未定义的属性'author',但仅当导航到前面描述的/research时。如果我从/direction执行相同操作,我将得到相同的错误,但如果我尝试从/research导航到任何路线,它都可以正常工作):

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

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

看起来您的HREF有一些问题

li
              a(href='research')
                | Researchers
应该是

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

相对于您的根文件夹。

您的hrefs似乎有一些问题

li
              a(href='research')
                | Researchers
应该是

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

相对于您的根文件夹。

问题将出现在渲染代码中的某个位置,该位置未显示。您能否显示创建此错误链接到/articles/research的渲染代码的顺序?@ChrisPhillips添加了其他代码片段。您也可以共享articles-pug吗?@GiladArtzi添加了article.pug。如何从文章页面到研究?我在文章页面上看不到任何链接。问题将出现在某个地方的呈现代码中,没有显示。你能显示创建到/articles/research的坏链接的呈现代码的顺序吗?@ChrisPhillips添加了额外的代码片段。你也能共享文章帕格吗?@GiladArtzi添加了article.pug.你如何从文章页面转到研究页面?我在文章页面上没有看到任何链接