Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
How to display all users database (mongodb) details on .ejs file

I have a node.js website that i build with this tutorial: https://scotch.io/tutorials/easy-node-authentication-setup-and-local

now i want in one of my views to print/display all users data.

in routes.js:

var User = require('../app/models/user'); //the connection to users database
app.get('/profile', isLoggedIn, function (req, res) {
    res.render('profile.ejs', {
        Users: User // get the user out of session and pass to template
    });
});

and this is in my profile.ejs file:

<ul>
<% Users.forEach( function(err, user) { %>
<li><%= user %> </li>
<% }); %>

i get an error: "undefined is not a function" because of the <% Users.forEach( function(err, user) { %> line

what am i doing wrong?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to actually query the user collection via the model's find() query function which will return an array of user documents when executed, i.e.

var User = require('../app/models/user'); //the connection to users database
app.get('/profile', isLoggedIn, function (req, res) {
    User.find({}).exec(function(err, users) {   
        if (err) throw err;
        res.render('profile.ejs', { "users": users });
    }
});

then render the list of user documents in your profile.ejs as:

<% users.forEach(function (user) { %>
    <li>  
        <h1><%= user.name %></h1>  
    </li>                                   
<% }) %>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda