phouse512
phouse512

Reputation: 680

handlebars js not rendering array

I've been working for a while to try and render an array from an express app that I have running but I get no errors but nothing displayed.

Here is my handlebars template:

<h3>Registered Users:</h3>
<h5>{{users}}</h5>
<ul class="list-group">
    {{#each users}}
    <li class="list-group-item">
        {{this.fields.username}}
    </li>

    {{/each}}
</ul>

Here is my express code that returns users.

/* GET users listing. */
router.get('/', function(req, res, next) {
  api.getUsers(function(response, body){
    console.log(body);
    res.render('users/list', { 'users': body });
  });
});

At first I thought the users array wasn't getting passed through from express correctly, but in the template you can see that I just displayed users, and the object displayed was what I thought it would be.

But the list of users is not getting rendered, even though there's at least one element in the array. Any ideas? I'm sure it's something small I'm missing..

Upvotes: 0

Views: 989

Answers (1)

AfDev
AfDev

Reputation: 1242

I would have to agree with @Joe Attardi, your template should look more like this:

<h3>Registered Users:</h3>
<h5>{{users}}</h5>
<ul class="list-group">
    {{#each users as |user|}} <!-- get each user as user from users -->
    <li class="list-group-item">
        {{user.username}}
    </li>
    {{/each}}
</ul>

Upvotes: 3

Related Questions