简体   繁体   中英

Sails JS - Display flash message doesn't work on Heroku(production) but works fine in development

The flash message only works on my local machine during development...It doesn't work when I deployed the app on Heroku. I've been trying to find the answer but couldn't find anything that solves my problem.

api/policies/flash.js

module.exports = function(req, res, next) {
    res.locals.flash = {};
    if(!req.session.flash) return next();
    res.locals.flash = _.clone(req.session.flash);

    // clear flash
    req.session.flash = {};

    next();
};

api/controller/UserController.js (within create action) - When the form is submitted successfully, redirect to the homepage and display thank you message.

res.redirect("/");
req.flash('signup-message', '<div class="thankyou-message-wrapper bg-success"><span class="glyphicon glyphicon-remove pull-right" aria-hidden="true"></span><span class="thankyou-message">Thank you for submitting the form!<br> Our administer will review your submission and publish soon.</span></div>');

view/main/index.ejs - This is how I render the message from the UserController.js

<%- req.flash('signup-message') %>

Does anyone have any insight as to why the flash message is not showing up when I deployed on Heroku?

You will need to add the policy to the routes.js file to enable a policy for the homepage. For example:

 '/': {
      view: 'homepage', policy: 'flash'
 }

Then in your UserController, add your message to session.flash:

 req.session.flash = {
      message : '<div class="thankyou-message-wrapper bg-success"><span class="glyphicon glyphicon-remove pull-right" aria-hidden="true"></span><span class="thankyou-message">Thank you for submitting the form!<br> Our administer will review your submission and publish soon.</span></div>'
 }

Finally, display the message in your homepage, first check if a message exists and then display:

 <% if(flash && flash.message) { %>
      <%- flash.message %>
 <% } %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM