I need to restrict editing and deleting options on a list of events to the creators of those events only.
I managed to fetch the currentUser._id when submitting the form and save it on my mongoDB, but I now face the problem of how to compare this to the session.currentUser._id in order to match them and use them in my {{#if}} in handlebars. Does anyone have any input on how to approach this? below is my JS and hbs for reference.
router.get('/events-all', (req, res, next) => { res.render('events/events-all'); }); router.post('/new-event', (req, res, next) => { const host = req.session.currentUser._id; const { title, location, date, time } = req.body; const newEvent = new Event({ title, location, date, time, host }); newEvent.save() .then(() => { res.redirect('events-all'); }) .catch((error) => { console.log('Error while adding event', error); }); });{{#if (host === currentUser._id) }}
<section> <article> <h3>{{location}}</h3> <picture> <img src="/images/citydefult.jpeg" alt="" /> </picture> <h5>{{title}}</h5> <h6>{{date}}</h6> <p>Starting time: {{time}}</p> <a href="/events/{{id}}/edit"> <button type="submit">Edit</button> </a> <form method="POST" action="/events/{{id}}/delete"> <button type="submit">Delete</button> </form> </article>
</section>
{{else}}
<section> <article> <h3>{{location}}</h3> <picture> <img src="/images/citydefult.jpeg" alt="" /> </picture> <h5>{{title}}</h5> <h6>{{date}}</h6> <p>Starting time: {{time}}</p> </form> </article>
</section>
{{/if}} 8 Related questions 1 How to use equality in handlebars build-in #if helper 5 How to compare a value in handlebars? 0 Want to compare two string in handlebars view in node js app Related questions 1 How to use equality in handlebars build-in #if helper 5 How to compare a value in handlebars? 0 Want to compare two string in handlebars view in node js app 5 Error: Missing helper: "if_equal" handlebars 0 Handlebars disable attribute if id exist 0 handlebars equality check in if/else block 1 How to compare the value of a variable in handlebars 1 Not able to compare Object Id using handlebars? 0 user id not the same as user post id 0 Rendering User ID ExpressJS Load 7 more related questions Show fewer related questions Reset to default