【问题标题】:EJS failing to render templateEJS 无法渲染模板
【发布时间】:2016-02-26 04:40:18
【问题描述】:

我正在尝试渲染一个包含以下代码块的模板:

<% 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> <% } %>
 <% } %>

但是,当模板尝试渲染时,如果上述变量之一实际上是未定义的,它会抛出一个变量“未定义”的错误。

捕获未定义变量的 if 语句的目的是消除此错误的发生,但是即使我正在检查变量是否未定义,似乎仍然会引发错误。有谁知道这可能是为什么?非常感谢!

【问题讨论】:

    标签: javascript html ejs


    【解决方案1】:

    你需要使用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?

    【讨论】:

    • 成功了!感谢您的链接。不知道我是怎么错过那篇文章的。
    猜你喜欢
    • 2012-05-08
    • 2015-07-17
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 2014-01-24
    相关资源
    最近更新 更多