Sunday 20 October 2019

Having trouble hiding button inside my pug file

Hi there, im truly appreciate it if anyone can help me towards my problem or point where my mistake is. Currently I wanted to hide a button inside my pug file if certain condition is met. The condition I set inside my pug file is that if the user.id is equal to the tournament.author, only show the button if its belongs to the user otherwise hide. I created this condition is to not let other user edit or delete the tournament post. Below are some code I provide to help anyone identify what did I missed.​mongodb sample data> db.tournaments.find().pretty() { "_id" : ObjectId("5dab977fcec7714424b1ad0f"), "date" : ISODate("2019-10-19T23:08:47.521Z"), "title" : "Post1", "author" : "5dab7d10b9b5c73de0ed0ea2", <--- should be the same with user.id > db.users.find().pretty() { "_id" : ObjectId("5dab7d10b9b5c73de0ed0ea2"), <---- should be the same with tournament.author "date" : ISODate("2019-10-19T21:16:00.541Z"), app.jsapp.get('*', function(req, res, next){ res.locals.user = req.user || null; next(); }); tournament.js//Show Single Tournament router.get('/:id', function(req, res){ Tournament.findById(req.params.id, function(err, tournament){ User.findById(tournament.author, function(err, user){ res.render('tournament',{ tournament: tournament, author: user.username, }); }); }); }); //Submit To Create Tournament router.post('/create', ensureAuthenticated, function(req, res){ req.checkBody('title', 'Title is required').notEmpty(); req.checkBody('size', 'Size is required').notEmpty(); req.checkBody('type', 'Type is required').notEmpty(); req.checkBody('body','Body is required').notEmpty(); let errors = req.validationErrors(); if(errors){ res.render('create_tournament', { errors: errors, user: req.user, }); } else{ let tournament = new Tournament(); tournament.title = req.body.title; tournament.author = req.user._id; tournament.size = req.body.size; tournament.type = req.body.type; tournament.body = req.body.body; tournament.save(function(err){ if(err){ console.log(err); return; } else{ req.flash('success', 'Tournament created'); res.redirect('/'); } }); } }); //Submit Edited Tournament router.post('/edit/:id', ensureAuthenticated, function(req, res){ let tournament = {}; tournament.title = req.body.title; tournament.author = req.user._id; tournament.body = req.body.body; let query = { _id:req.params.id } Tournament.update(query, tournament, function(err){ if(err){ console.log(err); return; } else{ req.flash('success', 'Tournament updated'); res.redirect('/'); } }); }); tournament.pug (example screenshot).card-body strong Player Slots: |   #{tournament.size} br strong Bracket Format: |   #{tournament.type} p.card-text |!{tournament.body} p.card-text small.text-muted #{tournament.date} by #{author} if user .btn-group.float-right if user.id == tournament.author a.btn.btn-outline-dark.btn-sm(href='/tournament/edit/'+tournament._id) Edit a.btn.btn-outline-dark.btn-sm.delete-tournament(href='#', data-id=tournament._id) Delete

Submitted October 20, 2019 at 12:59PM by exspade

No comments:

Post a Comment