I've been encountering an issue where I am unable to properly link my comments to the message id. Below is what I have attempted:
Routes file:
router.get('/home', function(req, res){
if(req.cookies.user_id){
knex.raw(`SELECT * FROM users WHERE id = ${req.cookies.user_id}`)
.then(function(user){
knex.raw(`SELECT * FROM messages JOIN users on users.id =
messages.user_id`)
.then((joinInfo)=>{
knex.raw(`SELECT comments.id, users.username, messages.id
as mess_id, messages.body, comments.message_id, users.id
as us_id, comments.commentsbody, comments.created_at,
comments.updated_at FROM comments JOIN messages ON
comments.message_id = messages.id JOIN users on
comments.user_id = users.id`)
.then((commentInfo)=>{
res.render('loggedInHome', {user: user.rows[0],
messagedata: joinInfo.rows, title: 'Seddit', commentInfo:
commentInfo.rows})
})
});
}).catch((err)=>{
console.log(err, 'ERRRRR')
res.redirect('/')
})
}
)
EJS views page
<main class='main_content'>
<% for (var j = 0; j < messagedata.length; j++) { %>
<div class='message_box'>
<p><strong><%= messagedata[j].username %></strong>: <%=
messagedata[j].body %></p>
<% for (var i = 0; i < commentInfo.length; i++) { %>
<% if (commentInfo.mess_id == messagedata.comment_id) { %>
<p><strong><%= commentInfo[i].username %>
comments</strong>: <%= commentInfo[i].commentsbody %></p>
<% } %>
<% } %>
<% } %>
</div>
</main>
When viewing the information on localhost, it seems to be looping excessively and when I log the if statement, it often returns as undefined.