简体   繁体   English

如果条件不起作用,则EJS

[英]EJS if conditional doesnt work

I am currently trying to use ejs template in my express application. 我目前正在尝试在我的快速应用程序中使用ejs模板。 As indicated in the offical page of the template ( https://www.npmjs.com/package/ejs ) I am using if conditional to output variable if it has been defined . 如模板的官方页面( https://www.npmjs.com/package/ejs )所示,如果条件变量已定义,则使用if条件。 Like this 像这样

  <% if (msg) { %>
  <h2><%=msg %></h2>
  <% } else {%>
  <h2>There is no messages</h2>
  <% } %>

Unfortunately, everytime I do this server returns 500 error . 不幸的是,每次我执行此服务器都会返回500 error。 How do I resolve this? 我该如何解决?

You can't use if (msg) - that's the bit that errors. 您不能使用if (msg) -这就是出错的地方。

Instead, check for existence some other way - by using typeof for instance: 相反,以其他方式检查是否存在-例如使用typeof

<% if (typeof msg != "undefined") { %>
<h2><%=msg %></h2>
<% } else {%>
<h2>There is no messages</h2>
<% } %>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM