Tuesday 25 June 2019

connect-flash help :(

I been trying to get connect-flash to work together with express-validator but for some weird reason it works 10% of the time (Validation message is displayed as html) , the other 90%, HBS isn't displaying anything to me.I read somewhere this has to do with sessions could kindly use some advice. Also, I m really new to node so if my convention is weird or funny, do let me know!​Index​app.use(cookieParser(keys.cookieSecret)) //express session middleware app.use(session({ cookie : { maxAge : 1000 * 60 * 60 * 2, //2 hour session sameSite : true }, name : keys.SESS_NAME, resave : false, saveUninitialized : false, secret : keys.SESS_SECRET })) app.use(flash()); ​​Routerouter.post("/create", authAdmin, announcementValidation, async(req,res) => { if (req.errors.length != 0 ) { req.flash('announcementFail', req.errors); res.redirect("/dashboard") } else { req.flash('announcementSucceed', "Announcement Created") req.session.title = req.body.title; console.log("No errors present") const annoucement = new Announcement(req.body) annoucement.save().then(ann => { req.session.announcement = true; res.redirect("/dashboard") }) } }) announcementValidation.js​const {check, validationResult} = require('express-validator') announcementValidation = [ check('title').not().isEmpty().withMessage('title cannot be empty').trim().isLength({min : 5}).withMessage("title should at least be 5 characters long"), check('body').not().isEmpty().withMessage('body cannot be empty').trim(), (req,res,next) => { req.errors = validationResult(req).errors; next(); } ]; module.exports = announcementValidation Redirects to/dashboard//Display Dashboard Page router.get("/dashboard", authAdmin, async(req,res) => { //code not shown for brevity //just getting data for announcements and categories res.render('admin/dashboard', {announcementSucceed : req.flash('announcementSucceed'), announcementFail : req.flash('announcementFail'), announcements : announcementMap, categories : categories }); }) }) (is this bad convention? to display so many things with render)​dashboard.hbs
This is text ​​Any help would be much appreciated

Submitted June 25, 2019 at 11:02AM by Possible_Hawk

No comments:

Post a Comment