From req.flash() I got object { error: ['Invalid username'] }. I pass it to templates written in Jade using dynamic helpers. But when I do
for type in flashMessages
p #{type}
in the template I got the message (‘Invalid username’) instead of the type in variable type.
Can you give me an advice how to get the type please? I would like to print flash messages like this:
for type in flashMessages
each message in flashMessages[type]
p.alert-#{type} #{message}
Thank you
req.flash()is intended to either give you a flat list of all your messages, or a flat list of all messages of a particular type. Either way, the message objects themselves don’t tell you the type, cause it’s assumed you already knew that. Given thatreq.flash()is backed byreq.session.flash, however, you can roll your own functionality.First, you’ll need to prepare your own list of messages that have the information you want, so that you can pass it to your view. E.g.,
Second, make sure you make
allMessagesavailable within your template. E.g.,This should give you something you can iterate over within your view like this: