首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SyntaxError:在编译ejs时,在中的参数列表之后

SyntaxError:在编译ejs时,在中的参数列表之后
EN

Stack Overflow用户
提问于 2019-04-07 00:46:32
回答 1查看 8.9K关注 0票数 4

在编译ejs时,我在参数列表之后得到错误: missing )。我试了很多次,但还是找不到问题所在。

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

代码语言:javascript
复制
<%- 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') %>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-07 00:54:01

在关闭</tr>之前,下面的代码行

代码语言:javascript
复制
<% } %>

是多余的。因此,解析器将其解释为结束forEach()的回调函数,而不提供更多参数或结束函数调用的圆括号。(如果你仔细想想,错误消息实际上很清楚是怎么回事。:)

顺便说一句,在结束的</tbody></table>后面还有两个额外的>

下面是一个可以放入https://ionicabizau.github.io/ejs-playground/中的有效的已修复代码示例

代码语言:javascript
复制
<%
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>>
<% } %>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55551264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档