Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using "if" to check variable before usage results in Error, Undefined #90

Closed
netpoetica opened this issue Dec 10, 2012 · 10 comments

Comments

@netpoetica
Copy link

@netpoetica netpoetica commented Dec 10, 2012

From the example in the README

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

This code, in all situations, always results in an error if you don't pass the data to the file. I am trying to use it as, let's say, a report about whether your password was valid - if it wasn't I would render the file with

res.render('login', { msg: "Error: your password has too many characters." });

But if the user were going to the login page and it hadn't yet been posted, I would just use:

res.render('login');

I could fix this by explicitly sending a msg = null to all routes, but I think that is silly.

Any advice on how to properly handle this? My guess is that what I am doing is somehow unconventional in node/ejs.

Thanks a bunch,

  • Keith
@ForbesLindesay

This comment has been minimized.

Copy link
Contributor

@ForbesLindesay ForbesLindesay commented Dec 10, 2012

For your if statement you need to use typeof:

<% if (typeof user == 'object' && user) { %>
...
<% } %>

The same is actually true of undefined variables in regular JavaScript.

@ForbesLindesay

This comment has been minimized.

Copy link
Contributor

@ForbesLindesay ForbesLindesay commented Dec 10, 2012

You could alternatively add the following if it's an express app:

app.use(function (req, res, next) {
  res.locals.user = null
  next()
})
@netpoetica

This comment has been minimized.

Copy link
Author

@netpoetica netpoetica commented Dec 10, 2012

Interesting. You are correct. For some reason I thought that being that it was "undefined", JavaScript would have treated it as such - should have known better! I thought, in the very least, I would be able to call

if(a !== undefined)

but after testing in fiddle, it appears this error occurs any time JavaScript does not find the value while going up the scope chain.

Thanks for clarifying and for both tips!

@netpoetica netpoetica closed this Dec 10, 2012
@ForbesLindesay

This comment has been minimized.

Copy link
Contributor

@ForbesLindesay ForbesLindesay commented Dec 10, 2012

No problem. It turns out there's a subtle difference between a variable you haven't defined and a variable that is equal to undefined

@tj

This comment has been minimized.

Copy link
Owner

@tj tj commented Dec 22, 2012

yup when you use with(obj){} obj.foo = null is effectively like a var declaration var foo;, which otherwise would error in non-template code as well

@Meliodus

This comment has been minimized.

Copy link

@Meliodus Meliodus commented Feb 3, 2018

Both types are object and values are same but for some reason not able to equalise. Its always goes to else part.
<% if(post[0].category_id == cate._id){ %>

@fofe00

This comment has been minimized.

Copy link

@fofe00 fofe00 commented Feb 3, 2018

it is better to use

<% if(typeof variable=='undefined)
{
your code........
}
%>

@pierreburton91

This comment has been minimized.

Copy link

@pierreburton91 pierreburton91 commented Aug 10, 2018

I'm actually having the same issue as @netpoetica.
Trying to render a template while not initializing a variable.
The check described works, but the template engine still looks through my statement and returns errors when outputing data.

 <% if (typeof article != undefined) {%> // Should not be rendered if article var is not passed to template

 >> 23|         <input type="text" value="<%= article.headline %>" />

    24|     <%} else {%>

article is not defined
@trevorobrien

This comment has been minimized.

Copy link

@trevorobrien trevorobrien commented Oct 5, 2018

It might be your syntax <% if (typeof locals.article !== 'undefined') { %> - pay attention to your operator syntax, != vs !==

Also because the variable may not exist use locals.

@carpiodanielj

This comment has been minimized.

Copy link

@carpiodanielj carpiodanielj commented Mar 26, 2019

For your if statement you need to use typeof:

<% if (typeof user == 'object' && user) { %>
...
<% } %>

The same is actually true of undefined variables in regular JavaScript.

You should better use:

<% if (typeof user != 'undefined') { %>
...
<% } %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants
You can’t perform that action at this time.