Question: forEach not working properly on EJS template

Question

forEach not working properly on EJS template

Answers 0
Added at 2016-12-21 01:12
Tags
Question

I was trying to grab all the students from the database collection and have them display row by row in the EJS template. I thought a forEach would be the best way to do this but it only displayed one entry when there were 6. Any advice on how I could display all entries would be greatly appreciated.

The table

   <table class="table col-md-4">
    <thead class="thead-inverse">
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Math Level</th>
        <th>English Level</th>
    </tr>
    </thead>
    <tbody>
    <% students.forEach(function(student){%>
    <tr>
    <td><%=student.firstname %></td>
    <td><%=student.lastname %></td>
    <td><%=student.english_level%></td>
    <td><%=student.math_level%></td>
    </tr>
    <%})%>

    </tbody>
</table>

In this GET request I fetched all the students from my database and the page on which they would be displayed

//INDEX - show all students
app.get("/students", function(req, res){
    // Get all students from DB
    Student.find({}, function(err, allStudents){
        if(err){
            console.log(err);
        } else {
            res.render("students",{students:allStudents});
            console.log(allStudents);
        }
    });
});
Answers
Source Show
◀ Wstecz