'Unexpected token '}' in D:\ .users.ejs while compiling ejs If the above error is not helpful, you may want to try EJS-Lint:https://github.com/RyanZim/

This is my controller file

async function getUsers(req, res, next) {
    try {
        const users = await User.find();
        res.render('users', {
            users: users
        })
        res.render('users')

    } catch (err) {
        next(err)

    }
    // res.render('users')

}

this is my users.ejs file

<%- include('./partials/header.ejs'); %>
<div class="manageUser-container">
   <div id="title">
      <h2>Manage Users</h2>
   </div>
   <div class="new-message-container new-user">
      <a href="#" onclick="openModal()">+</a>
   </div>
   <div id="users-table">
      <table>
         <thead>
            <tr>
               <th>Name</th>
               <th>Email</th>
               <th>Manage</th>
            </tr>
         </thead>
         <tbody id="users-table">
            -->
            <% users.forEach((user)=>{ %>
            <tr id="<%= user._id %>">
               <td class="name">
                  <% if( user.avatar){ %>
                  <img src="./uploads/avatars/<%= user.avatar  %>" />
                  <%=}else{  %>
                  <img src="./images/nophoto.png" alt="User" />
                  <%    } %>
                  <span><%= user.name %></span>
               </td>
               <td><%= user.email %></td>
               <td class="manage">
                  <img src="./images/trash.png" alt="Delete" />
               </td>
            </tr>
            -->
            <h1>Hello</h1>
            `enter code here`
            <% }) %>
         </tbody>
      </table>
   </div>
</div>
<%- include('./partials/addUserModal.ejs'); %>
</body>
</html>

and error show in browsers

Unexpected token '}' in D:\programming\nodejs\chat_application\views\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.

SyntaxError: Unexpected token '}' in D:\programming\nodejs\chat_application\views\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 () at Template.compile (D:\programming\nodejs\chat_application\node_modules\ejs\lib\ejs.js:662:12) at Object.compile (D:\programming\nodejs\chat_application\node_modules\ejs\lib\ejs.js:396:16) at handleCache (D:\programming\nodejs\chat_application\node_modules\ejs\lib\ejs.js:233:18) at tryHandleCache (D:\programming\nodejs\chat_application\node_modules\ejs\lib\ejs.js:272:16) at View.exports.renderFile [as engine] (D:\programming\nodejs\chat_application\node_modules\ejs\lib\ejs.js:489:10) at View.render (D:\programming\nodejs\chat_application\node_modules\express\lib\view.js:135:8) at tryRender (D:\programming\nodejs\chat_application\node_modules\express\lib\application.js:640:10) at Function.render (D:\programming\nodejs\chat_application\node_modules\express\lib\application.js:592:3) at ServerResponse.render (D:\programming\nodejs\chat_application\node_modules\express\lib\response.js:1017:7)

How can i solve this error.?



Solution 1:[1]

I can help you debug this issue, but you will have to resolve it yourself. Here are the steps:

  1. We know from the error message that it is inside the users.ejs
  2. And we also know that it has something to do with }
  3. Computers never lie to us.

So this users.ejs has several partial views, or include directive. To narrow down the problem: Just remove all include, see if the problem persist? (restart the server if needed)

if the problem gone, we know that the error is inside one of those include files.

If not, we continue the step, by looking at any }, remove it line by line, restart the server (just to make sure we get the latest code), watch if the error gone, repeat.

Solution 2:[2]

i created this error inside the users.ejs file

Here i declared = inside the ejs expression like :

<%= }else{ %>

But the solution is

 <% } else { %>

i remove = sign after <% because else is a condition not value if i want to print some value then use <%= sign... i think it will help full for many developer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jimmy
Solution 2 Tarikul islam