Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SyntaxError: Unexpected token '{' in __dirname\views\admin\users.ejs while compiling ejs

I am currently developing a nodejs app with CRUD operations for the administrative end. This is my table for admins to be able to edit all users. For some reason I am getting an error for Unexpected token ‘{‘ in my ejs but I don’t see any unclosed brackets or anything of the like. I am confounded. Please let me know if you see the issue here. I’m starting to think the error doesn’t have to do with my view and more so with my code. Although I still don’t see any issue in my code, a fresh set of eyes will hopefully solve my problem.

My ejs file:

<div class="card">
    <div class="card-header">
        <div class="row">
            <div class="col-8">
                <h4 class="font-weight-bold">Users</h4>
            </div>
        </div>
    </div>
    <div class="card-body">
        <table class="table table-hover">
            <thead class="table-dark">
                <tr>
                    <th scope="col">Name</th>
                    <th scope="col">Role</th>
                    <th scope="col">Content Creator</th>
                    <th scope="col">Email</th>
                    <th scope="col">Username</th>
                </tr>
            </thead>
            <tbody>
                <% if(users.length> 0 { %>
                    <% for (let user of users) { %>
                        <tr>
                            <th scope="row">
                                <%= user.name %>
                            </th>
                            <td>
                                <%= user.role %>
                            </td>
                            <td>
                                <%= user.platform %>
                            </td>
                            <td>
                                <%= user.email %>
                            </td>
                            <td>
                                <%= user.username %>
                            </td>
                            <td>
                                <a href="admin/update/<%= user._id %>" class="" btn btn-sm btn-warning>Update</a>
                                <a href="admin/delete/<%= user._id %>" class="" btn btn-sm btn-danger>Delete</a>
                            </td>
                        </tr>
                        <% } %>
                     <% } %>
            </tbody>
        </table>
    </div>
</div>
</div>

My controller:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const User = require('../models/User.model');

const getAllUsers = async (req, res) => {
    const list = await User.find().exec();
    res.render('admin/users', {
        users: list
    })
}


module.exports = { getAllUsers }

my route:

const express = require('express');
const router = express.Router();
const User = require('../models/User.model')
const { authRole, ensureAuthenticated, } = require('../config/auth');
const { getUser, getAllUsers, updateUser, deleteUser } = require('../controllers/user.Controller')
const { json } = require('express/lib/response');

router.get('/', ensureAuthenticated, authRole, (req, res) => {
    res.render('admin/admin')
})

router.get('/users', ensureAuthenticated, authRole, getAllUsers);

module.exports = router;

Also it may help to see the data I’m working with here:

[
  {
    role: 'Admin',
    _id: 6217f3160f28e8d7aca39742,
    name: 'Matt',
    platform: 'Haze0_o',
    username: 'anemail@email.com',
    email: 'anemail@email.com',
    date: 2022-02-24T21:05:26.885Z,
    __v: 0
  },
  {
    role: 'User',
    _id: 6217f3482d1c8dbce051bb43,
    name: 'test3',
    platform: 'test3',
    username: 'test3',
    email: 'anemail@email.com',
    date: 2022-02-24T21:06:16.430Z,
    __v: 0
  },
  {
    role: 'User',
    _id: 62181fd5f85ed0b68c82f33b,
    name: 'Jackie',
    platform: 'a platform',
    username: 'a username',
    email: 'anemail@email.com',
    date: 2022-02-25T00:16:21.618Z,
    __v: 0
  }
]

>Solution :

It appears you have a typo. You’re missing the closing bracket on your if statement.

This line:

<% if(users.length> 0 { %>

Should be:

<% if(users.length> 0) { %>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading