Connect Flash is not working in Express 4.16.3
ahmedmusawir opened this issue ยท 4 comments
ahmedmusawir commented
I've tried both connect-flash and connect-flash-plus both without any luck with my following simple express app. It is totally non-responsive. Here is my package.json:
{
"name": "vidjot",
"version": "1.0.0",
"main": "index.js",
"repository": "https://github.com/ahmedmusawir/vidjot.git",
"license": "MIT",
"dependencies": {
"connect-flash": "^0.1.1",
"connect-flash-plus": "^0.2.1",
"express": "^4.16.3",
"express-handlebars": "^3.0.0",
"express-session": "^1.15.6",
"method-override": "^3.0.0",
"mongoose": "^5.2.15"
}
and here is my index.js and template file.
const express = require('express');
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');
const methodOverride = require('method-override');
const session = require('express-session');
const flash = require('connect-flash');
const app = express();
//FOLLOWING REPLACES BODY PARSER IN EXPRESS 4+
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//METHOD OVERRIDE MIDDLEWARE
app.use(methodOverride('_method'));
//SESSION MIDDLEWARE
app.use(
session({
secret: 'secret',
resave: true,
saveUninitialized: true,
cookie: { maxAge: 60000 }
})
);
//FLASH MESSEGING MIDDLEWARE
app.use(flash());
//GLOBAL VARIABLES
app.use(function(req, res, next) {
res.locals.success_msg = req.flash('success_msg');
console.log(req.flash('success_msg'));
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
next();
});
...
...
//DELETE NOTE
app.delete('/notes/:id', async (req, res) => {
Notes.remove({ _id: req.params.id }).then(() => {
console.log(req.flash('success_msg'));
req.flash('succss_msg', 'Note has been Removed');
res.redirect('/notes');
});
});
My template file:
`
`
{{> _navbar }}
{{#if succss_msg}} <div class="alert alert-success">{{success_msg}}</div> {{/if}} {{#if error_msg}} <div class="alert alert-danger">{{error_msg}}</div> {{/if}} {{{body}}} </div> </body>
Plz let me know what am I doing wrong here ... Thanx in advance.
ahmedmusawir commented
Resolved the issue ...
chrisrichie25 commented
@ahmedmusawir I am facing a similar problem. How did you solve it?
alejosv commented
@ahmedmusawir you ask for help but don't provide the solution ... enhance its your collaborative culture !!
Manas-Nagelia commented
Can you provide the solution please?