How to send a flash message with res.redirect('/') in Sails?
When i check some condition in a controller then I want to redirect to another url, passing along a flash message.
I am new to Sails, so any help will be appreciated.
Controller action:
module.exports ={
index: function (req, res) {
if(req.param('key')){
req.flash('message', 'welcome key is present');
res.redirect('/view/');
} else {
req.flash('message', 'welcome key is not present');
res.redirect('/');
}
}
}
Thanks in advance.
Your code looks fine for the controller. In your view, you can access the flash message as req.flash('message'), so in an .ejs file for example it would be <%- req.flash('message') %>
What I find better for any redirect with data, is to set the http code to 307. It'll redirect with post/put/delete data.
req.flash('message');
res.redirect(307, '/');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With