0
votes
<% include partials/header.ejs %>
    <div class="table-wrapper">
        <% if (players.length > 0) {%>
            <table class="table table-hovered">
                <thead class="thead-dark">
                    <tr>
                        <th scope="col">ID</th>
                        <th scope="col">Image</th>
                        <th scope="col">First Name</th>
                        <th scope="col">Last Name</th>
                        <th scope="col">Position</th>
                        <th scope="col">Number</th>
                        <th scope="col">Username</th>
                        <th scope="col">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <% players.forEach((player, index) => { %>
                        <tr>
                            <th scope="row"><%= player.id %></th>
                            <td><img src="/assets/img/<%= player.image %>" class="rounded-circle player-img" alt=""></td>
                            <td><%= player.first_name %></td>
                            <td><%= player.last_name %></td>
                            <td><%= player.position %></td>
                            <td><%= player.number %></td>
                            <td>@<%= player.user_name %></td>
                            <td>
                                <a href="/edit/<%= player.id %>" target="_blank" rel="noopener" class="btn btn-sm btn-success">Edit</a>
                                <a href="/delete/<%= player.id %>" class="btn btn-sm btn-danger">Delete</a>
                            </td>
                        </tr>
                    <% }) %>
                </tbody>
            </table>
        <% } else { %>
            <p class="text-center">No players found. Go <a href="/add" >here</a> to add players.</p>
        <% } %>
    </div>
</div>
</body>
</html>

This is the error being displayed after running the .ejs file from terminal,

SyntaxError: Unexpected identifier in C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\views\index.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 (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:626:12) at Object.compile (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:366:16) at handleCache (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:215:18) at tryHandleCache (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:254:16) at View.exports.renderFile [as engine] (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:459:10) at View.render (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\view.js:135:8) at tryRender (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\application.js:640:10) at Function.render (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\application.js:592:3) at ServerResponse.render (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\response.js:1012:7)

I am new to expressjs and this a code from a demo app in GitHub. It would be a great help if this problem is solved.

1

1 Answers

0
votes

Read this document https://github.com/tj/ejs and i think your foreach loop having some problem check this first and then try once.

 <% for(var i=0; i<players.length;i++) { %>
                                <tr id="idrow_<%= players[i]._id%>">

                                    <td id="id">
                                        <%= players[i]._id%>
                                    </td>
                                    <td id="name">
                                        <%= players[i].first_name%>
                                    </td>
                                    <td id="phone">
                                        <%= players[i].last_name%>
                                    </td>
                                    <td id="email">
                                        <%= players[i].position%>
                                    </td>
                                    <td id="vehicle">
                                        <%= players[i].number%>
                                    </td>
                                    <td id="other">
                                        <%= players[i].user_name%>
                                    </td>

                                    <td><button type="button" class="btn btn-secondary" id="edtbtn" onclick="edituser('<%= players[i]._id %>')" name="edit"><b>edit</b></button></td>
                                    <td><button type="button" class="btn btn-secondary" id="delbtn" onclick="deleteuser('<%= players[i]._id %>')" name="delete">
                                    <span><strong>Delete</strong></span>
                                </button>
                                    </td>
                                </tr>
                                <% } %>

========================================================================