What is the correct way to check if a variable exists in an EJS template (using ExpressJS)?


Aashay Desai

On the EJS github page, there is only a simple example : https://github.com/visionmedia/ejs

example

<% if (user) { %>
    <h2><%= user.name %></h2>
<% } %>

This seems to be checking for the existence of a variable named user, and if so, do something. h, right?

My question is, why in the world does Node throw a ReferenceError if the user variable doesn't exist? This invalidates the example above. What is the proper way to check if a variable exists? Should I use a try/catch mechanism and catch that ReferenceError?

ReferenceError: user is not defined
    at IncomingMessage.anonymous (eval at <anonymous> (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:140:12))
    at IncomingMessage.<anonymous> (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:142:15)
    at Object.render (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:177:13)
    at ServerResponse.render (/usr/local/lib/node/.npm/express/1.0.7/package/lib/express/view.js:334:22)
    at Object.<anonymous> (/Users/me/Dropbox/Projects/myproject/server.js:188:9)
    at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)
    at pass (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:162:10)
    at /usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:152:27
    at Object.restrict (/Users/me/Dropbox/Projects/myproject/server.js:94:5)
    at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)

I know I can get rid of this error by adding a "user" local variable in the server code, but the whole point here is that I want to use your everyday if/else to check for the existence of such variables at runtime in a nullcheck type pattern. The exception seems ridiculous to me for a variable that doesn't exist.

Tjholowaychuk

You can do the same with anything in js typeof foo == 'undefined', or since "locals" is the name of the object that contains them if (locals.foo). It's just raw js :p

Related


What is the correct way to check if a global variable exists?

Ebrahim Byagowi: JSLint is not passing this as valid code: /* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } What is the correct way? bigoldbrute: /*global window */ if (window.someVar === undefined) { window.so

What is the correct way to check if a global variable exists?

Ebrahim Byagowi: JSLint is not passing this as valid code: /* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } What is the correct way? bigoldbrute: /*global window */ if (window.someVar === undefined) { window.so

What is the correct way to check if a global variable exists?

Ebrahim Byagowi: JSLint is not passing this as valid code: /* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } What is the correct way? bigoldbrute: /*global window */ if (window.someVar === undefined) { window.so

What is the correct way to check if an array exists in JS?

Hussein When I check the length of the array, I get the following error. What is the correct way? main file if (drugPrice.mailPrice.rejectMessage.length !== 0 && Array.isArray(drugPrice.mailPrice.rejectMessage)) { //code goes here } mistake TypeError: Can

What is the correct way to check if a collection exists?

Tim Previously, I checked if the collection existed by querying the namespace. Roughly like this, check if "foo.bar" exists: return 1 === $client->selectCollection('foo','system.namespaces'); ->count(['name'=>'bar']); Since this only works

What is the correct way to check if an array exists in JS?

Hussein When I check the length of the array, I get the following error. What is the correct way? main file if (drugPrice.mailPrice.rejectMessage.length !== 0 && Array.isArray(drugPrice.mailPrice.rejectMessage)) { //code goes here } mistake TypeError: Can

What is the correct way to check if a collection exists?

Tim Previously, I checked if the collection existed by querying the namespace. Roughly like this, check if "foo.bar" exists: return 1 === $client->selectCollection('foo','system.namespaces'); ->count(['name'=>'bar']); Since this only works

How to check if EJS variable exists?

Pranai Kumar I'm using EJS layouts in a Node.js application. Currently I'm having an issue where it just generates an error when the required data is not available in the EJS file. What I want is to add a condition before using the EJS variable in javascript.

Is this the correct way to check if a file exists?

beginner I'm trying to check if a file (image) exists so I can delete it before uploading a new one. Here's what I've tried so far: foreach (glob("../imgs/" . $name . ".*") as $file) { if (file_exists($file)) { unlink($file); } } Is this the c

Is this the correct way to check if a file exists?

beginner I'm trying to check if a file (image) exists so I can delete it before uploading a new one. Here's what I've tried so far: foreach (glob("../imgs/" . $name . ".*") as $file) { if (file_exists($file)) { unlink($file); } } Is this the c

Is this the correct way to check if a file exists?

beginner I'm trying to check if a file (image) exists so I can delete it before uploading a new one. Here's what I've tried so far: foreach (glob("../imgs/" . $name . ".*") as $file) { if (file_exists($file)) { unlink($file); } } Is this the c

What is the correct way to initialize a variable using typing

Kevin Mueller Our company started using Pythons Typing as it is available in the newer Python3 version. We're using the "mypy" linter to help with typos in our scripts. My problem occurs when I initialize empty variables in the script header like this: VARIABL

What is the correct way to use Nodemailer in expressjs?

Arambo I am trying to use nodemailer in expressjs application . Should I keep creating the transport object outside of the route handler, or is it just fine to create the transport object inside the route handler? var express = require('express') , app = exp

What is the correct way to use Nodemailer in expressjs?

Arambo I am trying to use nodemailer in expressjs application . Should I keep creating the transport object outside of the route handler, or is it just fine to create the transport object inside the route handler? var express = require('express') , app = exp

What is the correct way to use Nodemailer in expressjs?

Arambo I am trying to use nodemailer in expressjs application . Should I keep creating the transport object outside of the route handler, or is it just fine to create the transport object inside the route handler? var express = require('express') , app = exp