I want to use the flash
notification to show a successful message. I tried it as I mention below. When I tried the below code it doesn't show anything. How can I solve this?
page.js :
exports.create = async (req, res) => {
try {
req.flash('error_messages_test', 'Invalid credentials');
res.redirect('/page');
} catch (e) {
//code...
}
};
page.handlerbars :
<div>
{{#if error_messages_test}}
<div class="alert alert-danger">{{error_messages_test}}</div>
{{/if}}
</div>
app.js :
const flash = require('connect-flash');
app.use(flash());
You need to pass the "error_messages_test" variable to your view from the function that handles your "/page" route.
Try like this:
app.get('/page', function(req, res){
// other stuff
// Get flash messages by passing the key to req.flash()
res.render('page', { error_messages_test: req.flash('error_messages_test') });
});