SyntaxError:编译ejs时,在参数列表后缺少)

时间:2019-04-06 16:46:32

标签: javascript syntax-error ejs

编译ejs时,在参数列表后出现错误:缺少)。 我尝试了很多次,但是找不到问题。

这是导致错误的ejs。 这段代码有什么问题?

<%- 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 个答案:

答案 0 :(得分:1)

在结束</tr>之前,该行

<% } %>

是多余的。因此,解析器将其解释为结束forEach()的回调函数,而不提供进一步的参数或关闭 函数调用的圆括号。 (如果仔细考虑,错误消息实际上是很清楚的。:))

通过这种方式,您在结账></tbody>之后还得到了两个多余的</table>

这是一个可以工作的固定代码示例,您可以放入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>>
<% } %>