S
S
Sergey Burduzha2021-06-10 15:23:33
Express.js
Sergey Burduzha, 2021-06-10 15:23:33

Why is express-flash not working?

Good afternoon.
Connected flash and session

const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const flash = require('express-flash')
const session = require('express-session')

const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const categoryRouter = require('./routes/category');
const bookRouter = require('./routes/book');
const userRouter = require('./routes/user');
const issueBookRouter = require('./routes/issuebook')
const returnBookRouter = require('./routes/returnbook')
const settingsRouter = require('./routes/settings')

const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: 'secret_key'
}))
app.use(flash());


Then I created a message in the router

/* GET users listing. */
router.route('/admin/add-category')
  .get(function (req, res, next) {
    res.render('admin/add-category', { title: 'Express' });
  })
  .post(async function (req, res, next) {
    const { name, status } = req.body;
    try {
      const category = await CategoryModel;
      await category.create({ name, status })
      
      req.flash('message', 'success');
      res.render('/admin/add-category');
    } catch (e) {
      res.render('/admin/add-category');
    }
  });


And on the ejs page I display

<% if(message) { %>
        <div class="alert alert-success">
            <h2>Some success</h2>
        </div>
    <% } %>


And I get
message is not defined.

CdTzEwY.png

What did I miss?

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2021-06-10
@serii81

if (messages.message)judging by the documentation
https://github.com/RGBboy/express-flash/blob/maste...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question