I am building a student management system using Node.js, MySQL, and EJS to render views. I want to display a list of students with their status value in a table, but the status column isn’t showing any value, even though the data exists in the database.
MySQL Query:
SELECT s.stuNIC as NIC, s.stuUserName as username, u.intendedMajor, u.status
FROM student s
JOIN university_registration u ON s.stuNIC = u.NIC;
The query works correctly when I run it directly on MySQL, and it returns the status
value as expected.
EJS Template (uni-view-student.ejs
):
<% data.forEach(function(student) { %>
<tr onclick="window.location.href='/uni-notification/<%= student.NIC %>'">
<td><%= student.NIC %></td>
<td><%= student.username %></td>
<td><%= student.intendedMajor %></td>
<td><%= student.status %></td> <!-- This is not showing any value -->
</tr>
<% }); %>
Node.js Code (server.js
):
app.get('/uni-view-student', (req, res) => {
const query = `
SELECT s.stuNIC as NIC, s.stuUserName as username, u.intendedMajor, u.status
FROM student s
JOIN university_registration u ON s.stuNIC = u.NIC
`;
connection.query(query, (err, results) => {
if (err) {
console.error('Error querying the database:', err);
return res.status(500).send('Database query error');
}
res.render('uni-view-student', { data: results });
});
});
Even though student.status
is part of the query and has values in the database, the status column shows up empty on the page.
What could be causing this issue, and how can I fix it so that the status
value shows up correctly in the EJS template?