Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!