I have a route like this and works fine:

    // Dashboard route
    router.get('/dashboard', ensureAuthenticated, (req, res) => {
        Note.find({user: req.user.id})
        .then(notes => {
            res.render('index/dashboard', {
                notes: notes
            });
        });
    });

on the dashboard.handlebars I can loop the array with:

{{#if notes}}
...
    {{#each notes}}

        {{title}}
        {{body}} 

    {{/each}}
{{/if}}

I also have this other route, almost identical, but is not passing the array to the template:

    // Admin Route
    router.get('/admin', ensureAdmin, (req, res) => {
        User.find({})
            .then(users => {
                console.log("Users count is: " + users.length)
                console.log(users)
                res.render('index/admin'), {
                    users: users
                };
        });
    });

On the console I get both console.log working, I get the number of users and the info. So the Find() is working but the array seems like is not passed to the template.

I suspect it can be because I have a global var:

res.locals.user = req.user || null;

to keep the info of the logger user. But I changed the variables names on the Find() function and with no luck.

on the template I allways have "No users found" as result

{{#if users}}
  <p>There are users</p>
{{else}}
   <p>No users found</p>
{{/if}}

Any idea?

1 Answers

0
fedeteka On Best Solutions

Solved, it was a ")" on bad place...

'index/admin') was the cause

Solution is:

    // Admin Route
    router.get('/admin', ensureAdmin, (req, res) => {
        User.find({})
            .then(users => {
                console.log("Users count is: " + users.length)
                console.log(users)
                res.render('index/admin', {
                    users: users
            });
    });
});