9

I have a form submitting to an express app:

app.post('/myform', function (req, res) {
    var content = new registration(req.body);
    content.save(function(errObj){
    if(errObj){
        res.render('registration',{error: errObj});
    } else {
        res.render('thankyou');
    }
});

If there are any errors in saving the form data to mongoDb I want to display those errors at the top of the form so I'm passing the error object back to the ejs template:

<% if (error) { %>
   <li><%= error %></li>  
<% } %>

But when I initially load the page and there are no errors yet (because the form has not been submitted) I'm receiving an error on the page and the form is not rendered:

error is not defined

How can I get around this?

2
  • Why did you edit your question? You removed the relevant part Commented Aug 2, 2015 at 20:58
  • when you open the page with get method at first time, the error is also undefined Commented Jan 12, 2016 at 14:08

8 Answers 8

35

You can do that check in three ways :-

  • Using the method suggested by Amit :

res.render('registration', {error: false});

  • Check to see if error exists like this :

<% if( typeof(error) =="undefined")%> //checks the type of variable

  • You can see if the variable error exists by accessing the locals object like this :

<% if(locals.error) %>

Sign up to request clarification or add additional context in comments.

1 Comment

this should be the right answer. getting your variable from the locals object is preventing ejs from throwing the undefined error.
6

You could use a value in the error property that won't get through into the if:

res.render('registration', {error: false});

2 Comments

Of course! Thanks. Should have thought of that but still getting used to express/ejs
using ejs doesn't seem to work like using hbs in NestJS. Using hbs you can just return the variables as object, using EJS you can't.
5

The following code explains a working example:

<% if (typeof error ='object' && error) { %>
    <li><%= error %></li>  
<% } %>

Comments

2

Server: fast precessing

<% if (typeof data != undefined) { %>
   <li><%= data %></li>  
<% } %>

Comments

1

I was facing the same problem and you can try it in two different ways.

  1. Pass false to error on get from js file like below.
router.get('/login', function(req, res, next) {
    res.render('login', { title: 'Please login',  error: false });
});
  1. Another way is to handle it in ejs file, here you can check that error is passing from method call or not, like below.
<% if (typeof error != "undefined" && error.username) {  %>
    <p><%= error.username.msg %></p>
<%} %>

Comments

0

Try starting the server again by running the command $ node server.js

Note: server.js is a user-defined file.

1 Comment

I had to do this. My variables wasn't defined yet.
0

I know this was a couple of years ago at this point, but in case anyone else is stuck.

I resolved this issue by changing the scope of my variable(s) to global. Such an easy fix, but took me hours to figure out.

Instead of having the variable(s) in my app.post method, I moved everything to app.get. I then added res.redirect() to my app.post method and declared all my variable(s) at the top of my js file.

Comments

0

If you are setting some varibles later after performing an action then maybe you might consider setting them to null first. Lets say for example you want to render a page where you check cash balances after withdrawing some amount then on loading the page cash.ejs you'll get these undefined errors but you can fix this by initializing those variables to null first in the render function like this:

app.get("/cash", function (req,res){
    res.render ( "cash.ejs",{amount:"", charge:"", newbalance: ""});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.