I am very much beginner with node.js!
I am unable to handle json response in .ejs page. Its when user tries logging in with wrong details then I am rendering same page and sending some JSON response and checking if this sent response is not null then show some alert or something like "Unidentified details".
res.redirect('login', { LoginInvalid: "LoginInvalid" });
Using response in page-
<script>
if(JSON.stringify(<%-LoginInvalid%>)!=null){
console.log('invalid details');
}
</script>
But this script doesn't run and page shows me reference error with LoginInvalid is not defined
Please guid me troubleshooting this tiny issue.
You can't pass a variable to your ejs template if you are using res.redirect() (Read http://expressjs.com/api.html#res.redirect for more information). If you do want to pass some variable in it, the only way is to use GET query string like this
res.redirect('login?LoginInvalid=LoginInvalid');
And parse LoginInvalid query string from the login controller.
You can use flash messages with the express-flash module.
var flash = require('express-flash');
app.use(flash());
Then in your route,
req.flash('login', { LoginInvalid: "LoginInvalid" });
res.redirect('/login');
And in your view,
<% if (messages.login.LoginInvalid) { %>
<%= messages.login.LoginInvalid
<% } %>