I’m trying to make a hyperlink that grabs the post id from my database and creates a link based on the postID but it doesn’t seem to work
posts.ejs
<% for (let post of userPosts) { %>
<% if (currentTopic !== post.name) { %>
<li><strong><%= post.name %></strong></li>
<% currentTopic = post.name; %>
<% } %>
<li>
<a href="/article/<%= post.post_id %>"><%= post.post_name %> - <%= post.username %></a>
</li>
<% } %>
main.js
app.get('/article/:postId', function(req, res) {
const postId = req.params.postId;
const sqlQuery = 'SELECT * FROM posts WHERE post_id = ?';
db.query(sqlQuery, [postId], (err, result) => {
if (err || result.length === 0) {
// Handle error or post not found
res.send('Post not found.');
} else {
const post = result[0];
res.render('article.ejs', { post });
}
});
});
I can’t seem to find the issue
Database
*create posts table
CREATE TABLE `posts` (
`post_id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`topic_id` int NOT NULL,
`post_name` varchar(45) NOT NULL,
`article` varchar(1000) NOT NULL,
PRIMARY KEY (`post_id`),
UNIQUE KEY `post_id_UNIQUE` (`post_id`)
)
I’ve logged my errors down but i do not get anything when i click on the hyperlink