{"id":742,"date":"2022-08-30T15:06:25","date_gmt":"2022-08-30T15:06:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-would-you-check-for-undefined-property-in-ejs-for-node-js-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:06:25","modified_gmt":"2022-08-30T15:06:25","slug":"how-would-you-check-for-undefined-property-in-ejs-for-node-js-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-would-you-check-for-undefined-property-in-ejs-for-node-js-collection-of-common-programming-errors\/","title":{"rendered":"How would you check for undefined property in ejs for node.js?-Collection of common programming errors"},"content":{"rendered":"<p>What is the best way to check for an undefined property in an ejs template?<\/p>\n<p>(I&#8217;m using the node.js package by TJ Holowaychuk)<\/p>\n<p>Example:<\/p>\n<pre><code>var tpl = 'foo definedfoo undefined';\nconsole.log(ejs.render(tpl, { locals: { bar: \"baz\" } }));\n<\/code><\/pre>\n<p>I&#8217;d expect this to render &#8220;foo undefined&#8221;. It does throw an <em>foo undefined<\/em> error instead.<\/p>\n<p>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?<\/p>\n<p>The only solution I found is using the <code>hasOwnProperty<\/code> method.<\/p>\n<pre><code>var tpl = 'foo definedfoo undefined';\nconsole.log(ejs.render(tpl, { locals: { bar: \"baz\"} }));\n<\/code><\/pre>\n<p>This doesn&#8217;t throw any errors.<\/p>\n<p>Is there a better way to keep the template clean? Or why does it throw this error?<\/p>\n<ol>\n<li>\n<p>Another way to test for a property is to reference it indirectly via the <code>locals<\/code> object. Using your example:<\/p>\n<pre><code>var tpl = 'foo definedfoo undefined';\nconsole.log(ejs.render(tpl, { locals: { bar: \"baz\"} }));\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I would use <code>typeof<\/code>, as in <code>if (typeof foo == 'undefined')<\/code>. I use the <code>typeof<\/code> operator with the string &#8220;undefined&#8221;, whereas some people might do a direct comparison with the <code>undefined<\/code> 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.<\/p>\n<p>This could also be expressed as a ternary, which some view as &#8220;cleaner&#8221; due to the absence of curlies:<\/p>\n<pre><code>var tpl = 'foo definedfoo undefined';\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:22:37. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>What is the best way to check for an undefined property in an ejs template? (I&#8217;m using the node.js package by TJ Holowaychuk) Example: var tpl = &#8216;foo definedfoo undefined&#8217;; console.log(ejs.render(tpl, { locals: { bar: &#8220;baz&#8221; } })); I&#8217;d expect this to render &#8220;foo undefined&#8221;. It does throw an foo undefined error instead. I know [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-742","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=742"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/742\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}