最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Flash Message on redirect - Express - Stack Overflow

programmeradmin0浏览0评论

I cannot get my flash message to show when using a redirect (though all ok when using a render)

If i use this code to render a view the message appears fine

req.flash('success_msg', 'Successfully Registered');
res.locals.message = req.flash();
res.render('home');

but if i want to redirect (which i do in this instance) then the message is not displayed

req.flash('success_msg', 'Successfully Registered');
res.locals.message = req.flash();
res.redirect('/');

index.js

// Global Vars
app.use(function(req, res, next) {
  res.locals.success_msg = req.flash('success_msg');
  res.locals.error_msg = req.flash('error_msg');
  res.locals.error = req.flash('error');
  next();
});

// Use Routes
app.use('/', routes);
app.use('/', users);

Any ideas on how to handle this please, if i havent provided enough info please let me know what to add here

What i have noticed though is that i can log out the message before the redirect

req.flash('success_msg', 'Successfully Registered');
var message = res.locals.message = req.flash();
console.log(message); // { success_msg: [ 'Successfully Registered' ] }
res.redirect('/');

But it never shows in my view

<% if (locals.message) { %>
  <div class="alert alert-success alert-dismissible fade show text-center" role="alert">
    <strong><%= message.success_msg %></strong>
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
<% } %>

Thanks

I cannot get my flash message to show when using a redirect (though all ok when using a render)

If i use this code to render a view the message appears fine

req.flash('success_msg', 'Successfully Registered');
res.locals.message = req.flash();
res.render('home');

but if i want to redirect (which i do in this instance) then the message is not displayed

req.flash('success_msg', 'Successfully Registered');
res.locals.message = req.flash();
res.redirect('/');

index.js

// Global Vars
app.use(function(req, res, next) {
  res.locals.success_msg = req.flash('success_msg');
  res.locals.error_msg = req.flash('error_msg');
  res.locals.error = req.flash('error');
  next();
});

// Use Routes
app.use('/', routes);
app.use('/', users);

Any ideas on how to handle this please, if i havent provided enough info please let me know what to add here

What i have noticed though is that i can log out the message before the redirect

req.flash('success_msg', 'Successfully Registered');
var message = res.locals.message = req.flash();
console.log(message); // { success_msg: [ 'Successfully Registered' ] }
res.redirect('/');

But it never shows in my view

<% if (locals.message) { %>
  <div class="alert alert-success alert-dismissible fade show text-center" role="alert">
    <strong><%= message.success_msg %></strong>
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
<% } %>

Thanks

Share Improve this question edited May 1, 2018 at 21:25 Richlewis asked May 1, 2018 at 11:23 RichlewisRichlewis 15.4k39 gold badges137 silver badges300 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I'm pretty sure the problem is somewhere in the function for your global vars.

This works flawlessly here:

...
req.flash('msg', 'some msg');
res.redirect('/page');

And for the .get()

app.get('/page', (req, res) => {
    res.render('page', { flash: req.flash('msg') });
});

Be aware that everytime you get the flash content, it is deleted:

req.flash('msg', 'some msg');
console.log(req.flash('msg')); // prints 'some msg'
console.log(req.flash('msg')); // prints []

Also, I suggest you to check this very good gist from Brian MacArthur.

发布评论

评论列表(0)

  1. 暂无评论