Here’s my index.js
file:
const { faker } = require('@faker-js/faker');
const mysql = require("mysql2");
const express = require('express');
const app = express();
const path = require('path');
const { error } = require('console');
app.engine('ejs', require('ejs').__express);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'delta_app',
password: 'HarshitSingla@mysql'
});
let query = `INSERT INTO user (id, username, email, password) VALUES ?`;
app.get(`/`, (req, res)=>{
let q = `SELECT count(*) FROM user;`;
try {
connection.query(q, (error, result)=>{
if(error)throw error;
let count = result[0]["count(*)"];
res.render(`Home Page`, {count});
});
} catch (error) {
console.log(error);
res.send("Some error occured in DB!");
}
});
app.get(`/users`, (req, res)=>{
let q = "SELECT * FROM user";
try{
connection.query(q, (error, users)=>{
if(error)throw error;
res.render(`show users`, { users })
})
}catch(error){
console.log("Some error in DB was found!!");
}
});
app.listen("8080", ()=>{
console.log("App is listening to port 8080");
});
Here’s my show users.ejs
:
<title>Users List Page</title>
<h1>Here's the list of all the users: </h1>
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email ID</th>
</tr>
</table>
<% for(user of users){ %>
<tr>
<td><%= user.id %></td>
<td><%= user.username %></td>
<td><%= user.email %></td>
</tr>
<%= }%>
I can’t understand why am I getting this error:
PS C:UsersharshOneDriveDocumentsCodesTutorial FIlesApna College MERN StackNode with SQL> nodemon .index.js
[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node .index.js`
App is listening to port 8080
SyntaxError: Unexpected token '}' in C:UsersharshOneDriveDocumentsCodesTutorial FIlesApna College MERN StackNode with SQLviewsshow users.ejs while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
at new Function (<anonymous>)
at Template.compile (C:UsersharshOneDriveDocumentsCodesTutorial FIlesApna College MERN StackNode with SQLnode_modulesejslibejs.js:673:12)
at Object.compile (C:UsersharshOneDriveDocumentsCodesTutorial FIlesApna College MERN StackNode with SQLnode_modulesejslibejs.js:398:16)
at handleCache (C:UsersharshOneDriveDocumentsCodesTutorial FIlesApna College MERN StackNode with SQLnode_modulesejslibejs.js:235:18)
at tryHandleCache (C:UsersharshOneDriveDocumentsCodesTutorial FIlesApna College MERN StackNode with SQLnode_modulesejslibejs.js:274:16)
at exports.renderFile [as engine] (C:UsersharshOneDriveDocumentsCodesTutorial FIlesApna College MERN StackNode with SQLnode_modulesejslibejs.js:491:10)
at View.render (C:Usersharshnode_modulesexpresslibview.js:135:8)
at tryRender (C:Usersharshnode_modulesexpresslibapplication.js:657:10)
at Function.render (C:Usersharshnode_modulesexpresslibapplication.js:609:3)
at ServerResponse.render (C:Usersharshnode_modulesexpresslibresponse.js:1039:7)
I was expecting a simple table formed by the the EJS template from my database of 102 entries.
New contributor
Bada_Don is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1