Reputation: 135
I have Node app with Express and MongoDB. It has 3 routes: /articles, /directions and /research. All 3 routes renders titles from diferent collections in db and when i click on title it navigate to rest of data and path looks like this: http://localhost:3000/articles/59df896b7f13f25b9009c42e. When I try to navigate from that route to /directions by link it works and path looks like this: http://localhost:3000/directions as it should be. But when i try to navigate (from here http://localhost:3000/articles/59df896b7f13f25b9009c42e) to /research, path looks like this http://localhost:3000/articles/research and throws error. The correct path should be: http://localhost:3000/research. It don't work only if i try to navigate to /research. All 3 routes uses the same logic and code. I just replace articles with directions and so on.
So, my question is why app navigate to wrong path?
Code fragments - app/routes/directions.js, app/routes/articles.js and app/routes/researchers.js (I got TypeError: Cannot read property 'author' of undefined in // get one article, but only when navigate to /research as described earlier. If I do the same from /direction I'll get same error but if I try to navigate from /research to any route it works fine):
// 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}))))
Edited. Added additional code fragments.
Code fragments from 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
Upvotes: 0
Views: 47
Reputation: 4635
It looks like you have some problem with your hrefs.
li
a(href='research')
| Researchers
should be
li
a(href='/research')
| Researchers
to be relative to your root folder.
Upvotes: 2