express - attached comments loop to many times javascript -
i've been having error cant attach comments message id. below have tried, / hints great.
routes file:
router.get('/home', function(req, res){ if(req.cookies.user_id){ knex.raw(`select * users id = ${req.cookies.user_id}`) .then(function(user){ knex.raw(`select * messages join users on users.id = messages.user_id`) .then((joininfo)=>{ knex.raw(`select comments.id, users.username, messages.id mess_id, messages.body, comments.message_id, users.id us_id, comments.commentsbody, comments.created_at, comments.updated_at 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'> <% (var j = 0; j < messagedata.length; j++) { %> <div class='message_box'> <p><strong><%= messagedata[j].username %></strong>: <%= messagedata[j].body %></p> <% (var = 0; < commentinfo.length; i++) { %> <% if (commentinfo.mess_id == messagedata.comment_id) { %> <p><strong><%= commentinfo[i].username %> comments</strong>: <%= commentinfo[i].commentsbody %></p> <% } %> <% } %> <% } %> </div> </main>
on localhost when view info. looping many times , when console.log if statement responds undefined. i've spent hours rearranging things , can't work.
it's because putting 1 loop within another, inner loop show result times number of outer loop runs.
Comments
Post a Comment