user1189352 user1189352 - 21 days ago 5
Javascript Question

Error for variable in a *.ejs file

I'm following this guide right here: https://www.danielgynn.com/build-an-authentication-app-using-express-node-passport/ 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: http://imgur.com/a/hlChC

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) {
req.logout();
res.redirect('/');
});

router.post('/signup', passport.authenticate('local-signup', {
successRedirect: '/profile',
failureRedirect: '/signup',
failureFlash: true,
}));

router.post('/login', passport.authenticate('local-login', {
successRedirect: '/profile',
failureRedirect: '/login',
failureFlash: true,
}));


and lastly my passport.js file here: http://pastebin.com/swXJMPzi

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

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.

Comments