Giter Club home page Giter Club logo

Comments (2)

pronebird avatar pronebird commented on August 22, 2024

I think it always returns array.

from connect-flash.

isimmons avatar isimmons commented on August 22, 2024

Same here. If in your route or render method you set messages variable like

messages: req.flash()

Then you will get a messages variable in the rendered view which will be eqal to

{[]}

because req.flash() will still create an empty array.

So yes in your view you will need to check the length is not 0

I made a partial for flash messages that checks for both the existence and the length so I can include it at the top of all views

<% if(typeof messages !== 'undefined') { %>
<% if (typeof messages.error !== 'undefined' && messages.error.length > 0) { %>
    <p class="error"><%= messages.error %></p>
<% } %>
<% if (typeof messages.info !== 'undefined' && messages.info.length > 0) { %>
    <p class="info"><%= messages.info %></p>
<% } %>
<% if (typeof messages.success !== 'undefined' && messages.success.length > 0) { %>
    <p class="info"><%= messages.success %></p>
<% } %>
<% } %>

For ejs templates at top of body

<% include partials/flash.server.partial.html %>

This way I can just use req.flash() in my render function and every view will check for the existence of the 3 types of error messages I might or might not have flashed at some point in the process.

from connect-flash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.