Getting a flash message also clears it
blakehaswell opened this issue · 5 comments
It is expected that methods which return data should not have side effects. Currently, if I get data from the flash then that data is cleared:
req.flash('url');
// [ '/users/1']
req.flash('url');
// []
This leads to confusing code:
if (req.flash('url') && req.flash('url').length) {
// Never gets executed.
req.flash('url', req.flash('url'));
}
To get around this problem I need to add unnecessary temps to my methods and, worse, unnecessary properties to my methods.
There should be an explicit method to clear items from the flash, and getting them should not have side effects.
This is the long standing convention of the flash, as set by rails and implemented in express 2.x
It may be odd, but that is the intended behavior
Sent from my iPhone
On Aug 13, 2013, at 6:51 PM, Blake Haswell notifications@github.com wrote:
It is expected that methods which return data should not have side effects. Currently, if I get data from the flash then that data is cleared:
req.flash('url');
// [ '/users/1']req.flash('url');
// []
This leads to confusing code:if (req.flash('url') && req.flash('url').length) {
// Never gets executed.
req.flash('url', req.flash('url'));
}
To get around this problem I need to add unnecessary temps to my methods and, worse, unnecessary properties to my methods.There should be an explicit method to clear items from the flash, and getting them should not have side effects.
—
Reply to this email directly or view it on GitHub.
Thanks for the quick response. I understand that the behaviour is intentional, but the convention (however long standing) is bizarre. Any idea why this convention exists?
I have situations where I need to access the same value from the flash for different bits of logic, and now I need to pass arguments around when they should be unnecessary. 😔
Nope. If this doesn't fit your use case, don't use it.
Apologies, I have only just realised how flash is intended to be used. My experience with flash has been with Java’s Play! Framework, where the flash only exists for a single request.
Because I had that expectation I was trying to maintain the flash between requests when that was completely unnecessary. Thanks again for your quick responses.
Glad you figured it out!