Home > Software engineering >  Rendering User ID ExpressJS
Rendering User ID ExpressJS

Time:10-26

Currently using mongoose to render admin and user information on a dashboard however i can't seem to render the id of a user

Here is my code

function ensureAuthenticated(req, res, next){
  if(req.isAuthenticated()){
    return next();
  } else {
    req.flash('error_msg', 'You are not logged in');
    res.redirect('/dashboard/login');
  }
}

/* GET Dashboard page. */
router.get('/dashboard', ensureAuthenticated, (req, res) => {
  User.find({}, function(err, users) {
    res.render('dashboard/index.hbs', {
      pageTitle: 'Dashboard',
      total: users.length,
      users: users
    });
  });
});
<a href="/dashboard/users/{{_id}}">My profile</a>

Added some code to the question from the repo. Added the ensureAuthenticated function. From what I can see the model and everything is setup correctly and as an example the total renders correctly so it a getting users.

CodePudding user response:

When you debug the req that you have, something (I suspect Passport) has included a user variable directly there, so you can feed that into your view.

/* GET Dashboard page. */
router.get('/dashboard', ensureAuthenticated, (req, res) => {
  User.find({}, function(err, users) {
    res.render('dashboard/index.hbs', {
      pageTitle: 'Dashboard',
      total: users.length,
      users: users,
      currentUser: req.user
    });
  });
});

And in your view you can use that

<a href="/dashboard/users/{{currentUser._id}}">My profile</a>
  • Related