gpt4 book ai didi

javascript - 在特定路线之间移动时出错

转载 作者:行者123 更新时间:2023-12-01 02:55:59 25 4
gpt4 key购买 nike

我有带有 Express 和 MongoDB 的 Node 应用程序。它有 3 条路线:/articles、/directions 和/research。所有 3 条路线都会呈现数据库中不同集合的标题,当我单击标题时,它会导航到其余数据,路径如下所示: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})
}
}))

应用程序/ View /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

应用程序/ View /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

相对于您的根文件夹。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com