【发布时间】:2018-03-24 16:49:59
【问题描述】:
我有带有 Express 和 MongoDB 的 Node 应用程序。它有 3 条路线:/articles、/directions 和 /research。所有 3 条路线都呈现来自 db 中不同集合的标题,当我单击标题时,它导航到其余数据,路径如下所示: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: Cannot read property 'author' of undefined in // get one article,但是仅在如前所述导航到 /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
【问题讨论】:
-
那么问题将出现在某个地方的渲染代码中,没有显示出来。您能否显示创建此错误链接到 /articles/research 的渲染代码段?
-
@ChrisPhillips 添加了额外的代码片段。
-
哈巴狗的文章你也可以分享一下吗?
-
@GiladArtzi 添加了 article.pug。
-
如何从文章页面移动到研究页面?我在文章页面上没有看到任何链接
标签: javascript node.js mongodb express