I have tried many times but cannot resolve this syntax error. This is a snippet of the app.js(the starting point)
const express = require('express');
const sql = require('mysql');
const ejs = require('ejs');
const app = express();
app.set('view-engine','ejs');
app.use(express.urlencoded({extended: false}));
const db = sql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'assignment'
});
db.connect((err) => {
if (err)
throw err;
console.log('Mole in');
});
app.get('/show',(req,res) => {
let sql = 'SELECT * FROM info';
let query = db.query(sql,(err,result) => {
if(err) throw err;
console.log(result,result.length);
res.render('show.ejs',{
students : result
});
})
})
This is the snippet of the ejs file where I am inserting the data that I have received from the database
<tbody>
<%
students.forEach((student)=> {
%>
<tr>
<td><%=student['Name'] %></td>
<td><%=student['Age'] %></td>
<td><%=student['Gender'] %></td>
<td><%=student['Course'] %></td>
<td><%=student['Email'] %></td>
<td><%=student['Studentid'] %></td>
<td><%=student["Marks 1"] %></td>
<td><%=student["Marks 2"] %></td>
<td><%=student["Marks 3"] %></td>
<td><%=student["Marks 4"] %></td>
<td><%=student["Marks 5"] %></td>
<td>
<button class = "btn btn-outline-danger">Edit</button>
<button class = "btn btn-outline-danger">Delete</button>
</td>
</tr>
<% }); %>
</tbody>
I think there is no syntax error here in show.ejs.
The error is because you used js code outside ejs syntax. Also, since forEach has a single argument you dont have to wrap it with brackets.
<tbody>
<% students.forEach(student => {%>
<tr>
<td><%=student['Name'] %></td>
<td><%=student['Age'] %></td>
<td><%=student['Gender'] %></td>
<td><%=student['Course'] %></td>
<td><%=student['Email'] %></td>
<td><%=student['Studentid'] %></td>
<td><%=student["Marks 1"] %></td>
<td><%=student["Marks 2"] %></td>
<td><%=student["Marks 3"] %></td>
<td><%=student["Marks 4"] %></td>
<td><%=student["Marks 5"] %></td>
<td>
<button class = "btn btn-outline-danger">Edit</button>
<button class = "btn btn-outline-danger">Delete</button>
</td>
</tr>
<% }); %>
</tbody>