Javascript 你将如何检查 node.js 的 ejs 中未定义的属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7289916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would you check for undefined property in ejs for node.js?
提问by pvorb
What is the best way to check for an undefined property in an ejs template?
检查 ejs 模板中未定义属性的最佳方法是什么?
(I'm using the node.js packageby TJ Holowaychuk)
(我使用的是 TJ Holowaychuk的node.js 包)
Example:
例子:
var tpl = '<% if (foo) { %>foo defined<% } else { %>foo undefined<% } %>';
console.log(ejs.render(tpl, { locals: { bar: "baz" } }));
I'd expect this to render "foo undefined". It does throw an foo undefinederror instead.
我希望这会呈现“foo undefined”。它确实抛出了一个foo 未定义的错误。
I know that this is not supposed to be an issue, since this is expected behavior in the tests. Is there an easy way to avoid this?
我知道这不应该是一个问题,因为这是测试中的预期行为。有没有简单的方法可以避免这种情况?
The only solution I found is using the hasOwnProperty
method.
我找到的唯一解决方案是使用该hasOwnProperty
方法。
var tpl = '<% if (hasOwnProperty("foo")) { %>foo defined<% } else { %>foo undefined<% } %>';
console.log(ejs.render(tpl, { locals: { bar: "baz"} }));
This doesn't throw any errors.
这不会引发任何错误。
Is there a better way to keep the template clean? Or why does it throw this error?
有没有更好的方法来保持模板清洁?或者为什么会抛出这个错误?
回答by Richard Marr
Another way to test for a property is to reference it indirectly via the locals
object. Using your example:
另一种测试属性的方法是通过locals
对象间接引用它。使用您的示例:
var tpl = '<% if(locals.foo){ %>foo defined<% }else{ %>foo undefined<% } %>';
console.log(ejs.render(tpl, { locals: { bar: "baz"} }));
回答by Chris Baker
I would use typeof
, as in if (typeof foo == 'undefined')
. I use the typeof
operator with the string "undefined", whereas some people might do a direct comparison with the undefined
global variable. I prefer this method because it is protected against some terrorist JS library developer changing the value of the global variable, leaving your code broken.
我会使用typeof
,就像在if (typeof foo == 'undefined')
. 我使用typeof
带有字符串“undefined”的运算符,而有些人可能会直接与undefined
全局变量进行比较。我更喜欢这种方法,因为它可以防止某些恐怖的 JS 库开发人员更改全局变量的值,从而使您的代码损坏。
This could also be expressed as a ternary, which some view as "cleaner" due to the absence of curlies:
这也可以表示为三元,由于没有卷曲,有些人认为它“更干净”:
var tpl = '<% (typeof foo != "undefined" ? %>foo defined<% : %>foo undefined<% ) %>';
回答by rob_james
Simplest, and cleanest in my opinion:
在我看来,最简单、最干净:
<%= (!!locals.foo)?foo:'' %>
<%= (!!locals.foo)?foo:'' %>
回答by Subhradip D.
Earlier version of EJS supports only locals
But
Now in the latest versions of EJS support res.locals
早期版本的 EJS 只支持locals
但现在最新版本的 EJS 支持res.locals
Sample with ternary operator -
带有三元运算符的示例 -
<%= (res.locals.foo)?"foo":"" %>
Sample with if-else -
带有 if-else 的示例 -
<% if (res.locals.urvariable) { %>
<h1><%= urvariable.value1 %></h1>
<% } else { %>
<h1><%= urvariable.value2 %></h1>
<% } %>