{"id":727,"date":"2022-08-30T15:06:10","date_gmt":"2022-08-30T15:06:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/what-is-the-proper-way-to-check-for-existence-of-variable-in-an-ejs-template-using-expressjs-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:06:10","modified_gmt":"2022-08-30T15:06:10","slug":"what-is-the-proper-way-to-check-for-existence-of-variable-in-an-ejs-template-using-expressjs-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/what-is-the-proper-way-to-check-for-existence-of-variable-in-an-ejs-template-using-expressjs-collection-of-common-programming-errors\/","title":{"rendered":"What is the proper way to check for existence of variable in an EJS template (using ExpressJS)?-Collection of common programming errors"},"content":{"rendered":"<p>On the EJS github page, there is one and only one simple example: https:\/\/github.com\/visionmedia\/ejs<\/p>\n<p>Example<\/p>\n<pre><code>\n    <\/code><\/pre>\n<pre>\n\n<\/pre>\n<p><code>This seems to be checking for the existence of a variable named user, and if it exists, do some stuff. Duh, right?<\/code><\/p>\n<p><code>My question is, why in the world would Node throw a ReferenceError if the user variable doesn't exist? This renders the above example useless. What's the appropriate way to check for the existence of a variable? Am I expected to use a try\/catch mechanism and grab that ReferenceError?<\/code><\/p>\n<pre><code><code>ReferenceError: user is not defined\n    at IncomingMessage.anonymous (eval at  (\/usr\/local\/lib\/node\/.npm\/ejs\/0.3.1\/package\/lib\/ejs.js:140:12))\n    at IncomingMessage. (\/usr\/local\/lib\/node\/.npm\/ejs\/0.3.1\/package\/lib\/ejs.js:142:15)\n    at Object.render (\/usr\/local\/lib\/node\/.npm\/ejs\/0.3.1\/package\/lib\/ejs.js:177:13)\n    at ServerResponse.render (\/usr\/local\/lib\/node\/.npm\/express\/1.0.7\/package\/lib\/express\/view.js:334:22)\n    at Object. (\/Users\/me\/Dropbox\/Projects\/myproject\/server.js:188:9)\n    at param (\/usr\/local\/lib\/node\/.npm\/connect\/0.5.10\/package\/lib\/connect\/middleware\/router.js:146:21)\n    at pass (\/usr\/local\/lib\/node\/.npm\/connect\/0.5.10\/package\/lib\/connect\/middleware\/router.js:162:10)\n    at \/usr\/local\/lib\/node\/.npm\/connect\/0.5.10\/package\/lib\/connect\/middleware\/router.js:152:27\n    at Object.restrict (\/Users\/me\/Dropbox\/Projects\/myproject\/server.js:94:5)\n    at param (\/usr\/local\/lib\/node\/.npm\/connect\/0.5.10\/package\/lib\/connect\/middleware\/router.js:146:21)\n<\/code><\/code><\/pre>\n<p>I understand that I could make this error go away by simply adding a &#8220;user&#8221; local variable in my server code, but the whole point here is that I want to check for the existence of such variables at runtime using your every day if\/else nullcheck type pattern. An exception for a variable that doesn&#8217;t exist seems ridiculous to me.<\/p>\n<ol>\n<li>\n<p>how you would with anything in js, typeof foo == &#8216;undefined&#8217;, or since &#8220;locals&#8221; is the name of the object containing them, you can do if (locals.foo). it&#8217;s just raw js :p<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:44:08. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>On the EJS github page, there is one and only one simple example: https:\/\/github.com\/visionmedia\/ejs Example This seems to be checking for the existence of a variable named user, and if it exists, do some stuff. Duh, right? My question is, why in the world would Node throw a ReferenceError if the user variable doesn&#8217;t exist? [&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-727","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/727","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=727"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/727\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}