I'm using connect-flash with express to handle flash messages, but that messages are deleted when i read that properties, for example:
Defining flash message:
req.flash("myMessage", "Hey!");
Reading message:
console.log(req.flash("myMessage")); // Hey!
console.log(req.flash("myMessage")); // undefined
Data is erased on first read, and this is a problem, because i need to read the data of req.flash in a middleware to filter the http request and after in the controller (Which is in another file) i need to read the same req.flash data but i can't because in the read of the middleware was erased.
What can i do in this case?
The messages are stored on req.session.flash
internally, see the source code.
This means you can get the message by indexing directly into req.session.flash["myMessage"]
without it getting cleared.
This is technically an implementation detail of req.flash()
you shouldn't rely on, but I think if you pin the version of connect-flash
in your requirements it's safe enough.