Step 4 - Render the correct blog post based on post _id
Goal: When you click on Read More, it should take you to the post.ejs page rendering the correct post using the post._id
Scroll down if you need a hint 👇
- In home.ejs you’ll need to change the href of the anchor tag based on the post id instead of the post name.
<a href="/posts/<%=post._id%>">Read More</a>
- In the app.post() method for the /post route, you should change the express route parameter to postId instead.
app.get("/posts/:postId", function(req, res){
}
- You’ll need a constant to store the postId parameter value
const requestedPostId = req.params.postId;
- You can use the findOne() method to find the post with a matching id in the posts collection.
Post.findOne({_id: requestedPostId}, function(err, post){ }
- Once a matching post is found, you can render its title and content in the post.ejs page.
Post.findOne({_id: requestedPostId}, function(err, post){
res.render("post", {
title: post.title,
content: post.content
});
});
0 comments