Video Course

Create a CRUD App with Node and MongoDB: Lesson 4 of 17

All Events

Up Next

Single Events

Autoplaying in 7 seconds!

Cancel

Create a Free Account to Watch

Sign Up Now

Login to your account

Creating a list of events will be straightforward. This will mirror how we'll get information from our MongoDB database in the future.

const events = [
  { name: 'Basketball', slug: 'basketball', description: 'Throwing into a basket.' },
  { name: 'Swimming', slug: 'swimming', description: 'Michael Phelps is the fast fish.' },
  { name: 'Weightlifting', slug: 'weightlifting', description: 'Lifting heavy things up' }
];

To loop over these and show them in the browser, we'll pass these to ejs and show them:

// return a view with data
res.render('pages/events', { events: events });

And in our view:

<% for (var event of events) { %>
    <tr>
        <td><%= event.name %></td>
        <td><%= event.description %></td>
        <td>
            <a href="/events/<%= event.slug %>" class="btn btn-sm btn-primary">View</a>
        </td>
    </tr>
<% } %>

Chris Sevilleja

156 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.