user1189352 user1189352 - 1 year ago 68
Javascript Question

Error for variable in a *.ejs file

I'm following this guide right here: and everything works fine except for when I go to the login screen I get this error:

..... 7| >> 8| <% if (message != null) { %> 9| <%= message %> 10| <% } %> 11| message is not defined

picture of full error:

which i believe is referring to this line of code in the login.ejs file here:

<% if (message != null) { %>
<div class="alert-danger"><%= message %></div>
<% } %>

The thing is I don't see what's the problem with the code? The Signup page is EXACTLY identical. that should only display a message if the login is wrong anyway. I feel like that's such a standard line of code that shouldn't error in the first place?

If i comment those lines out it works fine.

some other parts of relevant code:

//index.js (router)
router.get('/login', function(req, res, next) {
res.render('login.ejs', { messag: req.flash('loginMessage') });

router.get('/logout', function(req, res) {
});'/signup', passport.authenticate('local-signup', {
successRedirect: '/profile',
failureRedirect: '/signup',
failureFlash: true,
}));'/login', passport.authenticate('local-login', {
successRedirect: '/profile',
failureRedirect: '/login',
failureFlash: true,

and lastly my passport.js file here:

The only thing that seems off is that in my passport.js file, is the req.flash message for my signup page is "signupMessage", but in my index.js the variable is "loginMessage" for both login and signup. BUT signup works fine.. displays the error jus fine.. login just won't display properly from the get go.

this is driving me nuts lol any help is appreciated

Answer Source

You have a typo:

res.render('login.ejs', { messag: req.flash('loginMessage') });

messag should be message:

res.render('login.ejs', { message: req.flash('loginMessage') });

Hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download