问题
I am getting the error: missing ) after argument list while compiling ejs. I tried many times, but I can't find the problem.
Here's the ejs that causes errors. What is the problem with this code?
<%- include('../_layouts/adminheader') %>
<h2 class='page-title'>Products</h2>
<br>
<a href="/admin/products/add-product" class="btn btn-primary">Add a new product</a>
<br><br>
<% if (count > 0) { %>
<table class="table table-striped">
<thead>
<tr class="home">
<th>Product</th>
<th>Price</th>
<th>Category</th>
<th>Product Image</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<% products.forEach(function(product) { %>
<tr>
<td><%= product.title %></td>
<td>$<%= parseFloat(product.price).toFixed(2) %></td>
<td><%= product.category %></td>
<td>
<% if (product.image == "") { %>
<img src="/images/noimage.png">
<% } else { %>
<img src="product_images/<%= product._id %>/<%= product.image %>">
<% }%>
</td>
<td><a href="/admin/products/edit-product/<%= product._id %>">Edit</a></td>
<td><a href="/admin/products/delete-product/<%= product._id %>" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a></td>
<% } %>
</tr>
<% }); %>
</tbody>>
</table>>
<% } else { %>
<h3 class="text-center">There are no products.</h3>>
<% } %>
<%- include('../_layouts/adminfooter') %>
回答1:
Before your closing </tr>
, the line
<% } %>
is superfluous. The parser therefore interprets this as ending the callback function of forEach()
without providing further arguments or closing the
round bracket of the function call. (The error message is actually pretty clear about what's going on, if you think about it. :))
By the way you also got two superflous >
behind your closing </tbody>
and </table>
.
Here's a working fixed code example you can put into https://ionicabizau.github.io/ejs-playground/
<%
var products = [
{title: "foobar", category: "things", image: "", _id: 1, price: 0}
];
var count = products.length;
%>
<h2 class='page-title'>Products</h2>
<br>
<a href="/admin/products/add-product" class="btn btn-primary">Add a new product</a>
<br><br>
<% if (products.length > 0) { %>
<table class="table table-striped">
<thead>
<tr class="home">
<th>Product</th>
<th>Price</th>
<th>Category</th>
<th>Product Image</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<% products.forEach(function(product) { %>
<tr>
<td><%= product.title %></td>
<td>$<%= parseFloat(product.price).toFixed(2) %></td>
<td><%= product.category %></td>
<td>
<% if (product.image == "") { %>
<img src="/images/noimage.png">
<% } else { %>
<img src="product_images/<%= product._id %>/<%= product.image %>">
<% }%>
</td>
<td><a href="/admin/products/edit-product/<%= product._id %>">Edit</a></td>
<td><a href="/admin/products/delete-product/<%= product._id %>" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a></td>
</tr>
<% }); %>
</tbody>
</table>
<% } else { %>
<h3 class="text-center">There are no products.</h3>>
<% } %>
来源:https://stackoverflow.com/questions/55551264/syntaxerror-missing-after-argument-list-in-while-compiling-ejs