Display usernames in view with MongoDB/Mongoose/Handlebars
<p>I have a small question about displaying usernames from my mongo/mongoose database in a HTML-page. So far I've written this code which prints out all the users from the database in my console, and it works just fine.</p>

<pre><code>User.find(function (err, users, res) {
    if (err) return console.error(err);
    console.log(users);
});
</code></pre>

<p>But I want to be able to print out the usernames in a neat little table on my HTML-page. Something like this:</p>

<pre><code>&lt;table class="table"&gt;
    &lt;tr&gt;
        &lt;td&gt;
            {{I want my users to be displayed here in a list.}}
        &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
</code></pre>

<p>I am using handlebars as my view engine. Any help towards solving this will be greatly appreciated since I've been stuck on this for a day now. If you need my userschema or anything else just tell me and I'll post it. Thanks</p>

<p>index.js</p>

<pre><code>var express = require('express');
var router = express.Router();
var app = express();


// Get Homepage
router.get('/', ensureAuthentication, function (req, res) {
    res.render('index');
});

// Get Assignments
router.get('/assignments', ensureAuthentication, function (req, res) {
    res.render('assignments');
});

// Get userlist
router.get('/userslist', ensureAuthentication, function (req, res) {
    res.render('userslist');
});


function ensureAuthentication(req, res, next) {
    if (req.isAuthenticated()) {
        return next();
    } else {
        res.redirect('/users/login');
    }
}

module.exports = router;
</code></pre>

<p>users.js</p>

<pre><code>var express = require('express');
var router = express.Router();
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var User = require('../models/user');

// Register
router.get('/register', function (req, res) {
    res.render('register');
});

// Login
router.get('/login', function (req, res) {
    res.render('login');
});

// Register User
router.post('/register', function (req, res) {

    var name = req.body.name;
    var email = req.body.email;
    var username = req.body.username;
    var password = req.body.password;
    var password2 = req.body.password2;

    // Validation
    req.checkBody('name', 'Name is required').notEmpty();
    req.checkBody('email', 'Email is required').notEmpty();
    req.checkBody('email', 'Email is not valid').isEmail();
    req.checkBody('username', 'Username is required').notEmpty();
    req.checkBody('password', 'Password is required').notEmpty();
    req.checkBody('password2', 'Passwords do not match').equals(req.body.password);

    var errors = req.validationErrors();

    if (errors) {
        res.render('register', {
            errors: errors
        });
    } else {
        var newUser = new User({
            name: name,
            email: email,
            username: username,
            password: password
        });

        User.createUser(newUser, function (err, user) {
            if (err) throw err;
            console.log(user);
        });

        req.flash('success_msg', 'You are now registered. Redirecting to login page.');

        res.redirect('/users/login');

    }
});

passport.use(new LocalStrategy(
  function (username, password, done) {
      User.getUserByUsername(username, function (err, user) {
          if (err) throw err;
          if (!user) {
              return done(null, false, { message: 'Unknown User' });
          }

          User.comparePassword(password, user.password, function (err, isMatch) {
              if (err) throw err;
              if (isMatch) {
                  return done(null, user);
              } else {
                  return done(null, false, { message: 'Invalid password' });
              }
          });
      });
  }));

passport.serializeUser(function (user, done) {
    done(null, user.id);
});

passport.deserializeUser(function (id, done) {
    User.getUserById(id, function (err, user) {
        done(err, user);
    });
});

router.post('/login',
  passport.authenticate('local', { successRedirect: '/', failureRedirect: '/users/login', failureFlash: true }),
  function (req, res) {
      res.redirect('/');
  });

router.get('/logout', function (req, res) {
    req.logout();

    req.flash('success_msg', 'Logged out successfully.');

    res.redirect('/users/login');
});


// Get users
User.find(function (err, users, res) {
    if (err) return console.error(err);
    res.render('users', { userList: users });
    //console.log(users);
});


module.exports = router;
</code></pre>