EJS 无法呈现模板
EJS failing to render template
我正在尝试呈现包含此代码块的模板:
<% if(type === 'Not Within Specifications'){ %>
<% if(Length !== undefined) { %><h5>Length: <%= Length %> </h5> <% } %>
<% if(Width !== undefined) { %><h5>Width: <%= Width %> </h5> <% } %>
<% if(thickness !== undefined) { %><h5>Thickness: <%= thickness %> </h5> <% } %>
<% } %>
然而,当模板尝试呈现时,如果上述变量之一实际上未定义,则会抛出变量为 "undefined" 的错误。
if 语句捕获未定义变量的目的是消除此错误的发生,但即使我正在检查变量是否未定义,似乎仍会抛出错误。有谁知道为什么会这样?非常感谢!
你需要使用typeof
:
<% if(type === 'Not Within Specifications'){ %>
<% if(typeof Length !== 'undefined') { %><h5>Length: <%= Length %> </h5> <% } %>
<% if(typeof Width !== 'undefined') { %><h5>Width: <%= Width %> </h5> <% } %>
<% if(typeof thickness !== 'undefined') { %><h5>Thickness: <%= thickness %> </h5> <% } %>
<% } %>
另请参阅此相关问题:How would you check for undefined property in ejs for node.js?
我正在尝试呈现包含此代码块的模板:
<% if(type === 'Not Within Specifications'){ %>
<% if(Length !== undefined) { %><h5>Length: <%= Length %> </h5> <% } %>
<% if(Width !== undefined) { %><h5>Width: <%= Width %> </h5> <% } %>
<% if(thickness !== undefined) { %><h5>Thickness: <%= thickness %> </h5> <% } %>
<% } %>
然而,当模板尝试呈现时,如果上述变量之一实际上未定义,则会抛出变量为 "undefined" 的错误。
if 语句捕获未定义变量的目的是消除此错误的发生,但即使我正在检查变量是否未定义,似乎仍会抛出错误。有谁知道为什么会这样?非常感谢!
你需要使用typeof
:
<% if(type === 'Not Within Specifications'){ %>
<% if(typeof Length !== 'undefined') { %><h5>Length: <%= Length %> </h5> <% } %>
<% if(typeof Width !== 'undefined') { %><h5>Width: <%= Width %> </h5> <% } %>
<% if(typeof thickness !== 'undefined') { %><h5>Thickness: <%= thickness %> </h5> <% } %>
<% } %>
另请参阅此相关问题:How would you check for undefined property in ejs for node.js?