我有此代码,它返回此错误

时间:2018-07-03 18:22:55

标签: node.js ejs

这是我的代码:

        <% if(errors){ %>
            <% errors.forEach(e => { %>
                <div class="alert alert-danger">
                    <%= e.msg %>
                </div>
            <% }); $>
        <% }; %>

我将其放在我的一个h2标签下,并且在有人问之前,是的,我确实传递了错误的参数。 现在该出错了

Error: Could not find matching close tag for "<%".
at C:\Users\dylda\Desktop\server_helper\node_modules\ejs\lib\ejs.js:693:19
at Array.forEach (<anonymous>)
at Template.generateSource (C:\Users\dylda\Desktop\server_helper\node_modules\ejs\lib\ejs.js:678:15)
at Template.compile (C:\Users\dylda\Desktop\server_helper\node_modules\ejs\lib\ejs.js:557:12)
at Object.compile (C:\Users\dylda\Desktop\server_helper\node_modules\ejs\lib\ejs.js:389:16)
at handleCache (C:\Users\dylda\Desktop\server_helper\node_modules\ejs\lib\ejs.js:212:18)
at tryHandleCache (C:\Users\dylda\Desktop\server_helper\node_modules\ejs\lib\ejs.js:251:16)
at View.exports.renderFile [as engine] (C:\Users\dylda\Desktop\server_helper\node_modules\ejs\lib\ejs.js:482:10)
at View.render (C:\Users\dylda\Desktop\server_helper\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\dylda\Desktop\server_helper\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\dylda\Desktop\server_helper\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\dylda\Desktop\server_helper\node_modules\express\lib\response.js:1008:7)
at C:\Users\dylda\Desktop\server_helper\routes\main.js:59:17
at Layer.handle [as handle_request] (C:\Users\dylda\Desktop\server_helper\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\dylda\Desktop\server_helper\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\dylda\Desktop\server_helper\node_modules\express\lib\router\route.js:112:3)    

所有其他标签都已关闭,因为当我摆脱此错误时,错误也会消失。

感谢所有的帮助

3 个答案:

答案 0 :(得分:2)

如果您看到错误,则会发现它显示 错误:找不到与“ <%” 匹配的关闭标签。

如果查看提供的代码,则会发现没有匹配的,关闭的“ <%”标记。

这应该解决它:

        <% if(errors){ %>
        <% errors.forEach(e => { %>
            <div class="alert alert-danger">
                <%= e.msg %>
            </div>
        <% }); %>  // Notice change here
    <% }; %>

答案 1 :(得分:1)

倒数第二行,您写的是$>而不是%>

<% if(errors){ %>
        <% errors.forEach(e => { %>
            <div class="alert alert-danger">
                <%= e.msg %>
            </div>
        <% }); %> // this line has a $>, instead of %>
    <% }; %>

答案 2 :(得分:0)

在ejs2文件中,您应该使用<$ $><% %><? ?><@ @> 因此,您必须使用其中之一。

<$ %>, `<@ ?>`

被视为错误