Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
Rendering User ID ExpressJS

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda