0
我一直有一個錯誤,我不能正確地將我的評論附加到消息ID。下面是我嘗試過的,任何幫助/提示都會很棒。附加評論循環多次javascript
routes文件:
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意見頁
<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>
在本地主機上,當我查看信息。它循環了很多次,當我console.log它通常回應未定義的if語句。我花了幾個小時重新整理了一些東西,卻無法讓它起作用。
問題到底在哪裏?控制檯中吐出的是什麼,返回數據的結構是什麼樣的? –
'console.log它通常用未定義響應的if語句 - 顯示你的控制檯的日誌/日期 –
它非常像一個reddit的克隆,所以用戶可以創建一條消息。我遇到的問題是,當我試圖顯示附加到該消息的註釋時,彈出註釋,但它們未附加到它們的特定消息。 – Jives