Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Aleks Dahlberg
Aleks Dahlberg
19,103 Points

Working with routing for Express and Pug (formally jade)

I am having trouble linking to a url for an individual post. Unsure if the problem is with jade or my express middleware.

pug - The a tag is the main focus here. The href should link to an individual post using the item.id as an identifier.

...
  ul
    each post in posts
      li
        a(href='/users/feed/#{post.id}') #{post.title}

express - getting a singular post works fine when the id of a post is put directly into a url bar (e.g copy and paste the id - localhost:5000/users/feed/ididididididididid) but does not work when clicking the pug href (instead the url bar show localhost:5000/users/feed/#{post.id})

The pug href does not even register the GET to /users/feed/:id, in the log is shows the GET as /users/feed/

//singular post routes
router.get('/feed/:id', function (req, res, next) {
  var id = req.params.id;
  Post.findById(id, function (err, post) {
    if (err) {
      next(err);
    }
    console.log(post);
    return res.render("post", { post:post })
  })
})

Can post the rest of the code if need be.

Any help greatly appreciated :)

1 Answer

Igor Yamshchykov
Igor Yamshchykov
24,397 Points

Try

a(href='/users/feed/' + post.id) #{post.title}

This might help

I'm not sure about it, but my guess is that jade doesn't compile href='/users/feed/#{post.id}' this kind of cause you either would need to make something like href='/users/feed/##{post.id}' as the hash sign is posible in href and this doesn't look very good, at least to me.

Aleks Dahlberg
Aleks Dahlberg
19,103 Points

As simple as that ae! Thanks a lot. I didn't think about that, strange all tutorials point to the way I did it, might be an outdated method.

Thanks again :)