You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EJS模板出现unexpected token语法报错,请求协助定位问题原因

EJS模板编译报错问题解决方案

报错根源

你代码里使用return()包裹HTML模板片段的写法不符合EJS语法规范,是本次报错的直接原因。
EJS编译模板时,会自动将<%%>标签外的HTML内容转换为字符串拼接的JS代码,你在脚本块中写return(后接HTML,会导致生成的JS代码结构混乱,多出不符合语法的分号,触发你遇到的Unexpected token报错。

其他存在的隐患

  • <th>标签内的<% history.sort %>没有加输出标记<%=,对应值不会被渲染到页面
  • status变量未声明var/let/const关键字,严格模式下会抛出引用错误

修正后的代码

<% if(user.account.deposits.length){ %>
  <% user.account.deposits.sort(sortFunction).map(history=>{ %>
    <% var className = history.approved ?  "text-success" : "text-warning" %>
    <% var status = history.approved ?  "approved" : "pending" %>
    <tr>
      <th scope="row"> <%= history.sort %> </th>
      <td class="<%= className %>"><%= status %></td>
      <td><%= history.amount %><b> $</b></td>
      <td> <%= history.date %></td>
    </tr>
  <% }) %>
<% } else { %>
  <tr>
    <th scope="row">#</th>
    <td class="text-light" colspan="3"> You have no deposit history</td>
  </tr>
<% } %>

内容的提问来源于stack exchange,提问作者Ebuka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 04:30:02