Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEthan M
10,637 PointsShowing what I added for Express Authentication course
Hello Treehouse, I've done the Express Authentication and it was absolutely perfect but there was one thing I desired to do and that was be able to search for a users profile based on the given id in the url so I am here to share the snippet if anyone else wants to try it out :)
// GET /profile/:id
router.get('/profile/:id?', function(req, res, next){
var id = req.params.id;
User.findById(id)
.exec(function(err, user){{
if (err) {
return next(err);
} else {
return res.render('profile',{
title: user.name,
name: user.name,
favorite: user.favoriteBook
})
}
}});
});
Basically I am taking the :id and putting it into a variable. You can name :id anything but the req.params.name must be the name you set for the :name. Now all you do is grab a users id and put it into the url after the /profile/ID and it will show the results on that user whether you are signed in or not :)
Ethan M
10,637 PointsEthan M
10,637 PointsThe question mark I believe makes it optional