Home > Software design >  SyntaxError: Unexpected token '{' in __dirname\views\admin\users.ejs while compiling ej
SyntaxError: Unexpected token '{' in __dirname\views\admin\users.ejs while compiling ej

Time:02-26

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 >
    <div >
        <div >
            <div >
                <h4 >Users</h4>
            </div>
        </div>
    </div>
    <div >
        <table >
            <thead >
                <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 %>"  btn btn-sm btn-warning>Update</a>
                                <a href="admin/delete/<%= user._id %>"  btn btn-sm btn-danger>Delete</a>
                            </td>
                        </tr>
                        <% } %>
                     <% } %>
            </tbody>
        </table>
    </div>
</div>
</div>

My controller:

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: '[email protected]',
    email: '[email protected]',
    date: 2022-02-24T21:05:26.885Z,
    __v: 0
  },
  {
    role: 'User',
    _id: 6217f3482d1c8dbce051bb43,
    name: 'test3',
    platform: 'test3',
    username: 'test3',
    email: '[email protected]',
    date: 2022-02-24T21:06:16.430Z,
    __v: 0
  },
  {
    role: 'User',
    _id: 62181fd5f85ed0b68c82f33b,
    name: 'Jackie',
    platform: 'a platform',
    username: 'a username',
    email: '[email protected]',
    date: 2022-02-25T00:16:21.618Z,
    __v: 0
  }
]

CodePudding user response:

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) { %>
  • Related