我在编译Ejs代码时遇到此错误

问题描述

此处obj是expressValidator的json解析文件

<%- include("partial/header") -%>

    <form method = "POST" action ="/">
        <% if(obj.name==="name1"){  %>
        <%= obj.msg =%>
        <% } %>
        <label> Name </label><br>
        <input type= "text" name ="name1">
        <br>
        <% if(obj.name==="place"){  %>
        <%= obj.msg =%>
        <% } %>
        <label> Place </label><br>
        <input type= "text" name ="place">
        <br>
        <% if(obj.name==="age"){  %>
        <%= obj.msg =%>
        <% } %>
        <label> Age </label><br>
        <input type= "text" name ="age">
        <br>
        <% if(obj.name==="time"){  %>
        <%= obj.msg =%>
        <% } %>
        <label> Time </label><br>
        <input type= "text" name ="time">
        <br>
        <div class="input-group">
  <div class="input-group-prepend">
    <span class="input-group-text" id="inputGroupFileAddon01">Upload</span>
  </div>
  <div class="custom-file">
    <input type="file" class="custom-file-input" id="inputGroupFile01"
      aria-describedby="inputGroupFileAddon01">
    <label class="custom-file-label" for="inputGroupFile01">Choose file</label>
  </div>
</div>
        <br>
        <input type="submit" value ="Submit">
    </form>

    <a class="button" href="/showelement">
    Show all elements</a>

<%- include("partial/footer") -%>

错误

SyntaxError: Unexpected token ) in /workspace/TestValidExpress/views/index2.ejs

在编译ejs时

如果上述错误没有帮助,则您可以尝试使用EJS-Lint: https://github.com/RyanZim/EJS-Lint 或者,如果您打算创建一个异步函数,请传递async: true作为选项。

    at new Function (<anonymous>)
    at Template.compile (/workspace/TestValidExpress/node_modules/ejs/lib/ejs.js:661:12)
    at Object.compile (/workspace/TestValidExpress/node_modules/ejs/lib/ejs.js:396:16)
    at handleCache (/workspace/TestValidExpress/node_modules/ejs/lib/ejs.js:233:18)
    at tryHandleCache (/workspace/TestValidExpress/node_modules/ejs/lib/ejs.js:272:16)
    at View.exports.renderFile [as engine] (/workspace/TestValidExpress/node_modules/ejs/lib/ejs.js:489:10)
    at View.render (/workspace/TestValidExpress/node_modules/express/lib/view.js:135:8)

解决方法

问题是您在ejs标记内使用了两个相等的=符号。
要输出呈现的值,请使用此<%= obj.msg %>格式, { {1}}

您可以阅读有关ejs标签here

的更多信息