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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:38:54  来源:igfitidea点击:

How would you check for undefined property in ejs for node.js?

javascriptnode.jsundefinedejs

提问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 hasOwnPropertymethod.

我找到的唯一解决方案是使用该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 localsobject. 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 typeofoperator with the string "undefined", whereas some people might do a direct comparison with the undefinedglobal 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 localsBut 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> 
<% } %>