flash
flash copied to clipboard
The value of a little advice
use while message = flash.shift() get value too cumbersome。 Some values are not my concern I just want error_message value Whether this can be req.flash('message') ?
I modified code after flash('message') the 'message' be delete, no need to manually shift.
function push(type, msg) {
var res = this.res || this;
var locals = res.locals.flash;
if(!msg){
if(typeof type === 'string' && locals.length){
for(var i = 0, l = locals.length; i < l; i++){
if(locals[i]['type'] === type){
msg = locals[i]['message'];
locals.splice(i, 1);
return msg;
}
}
}
return;
}
msg = {
message: msg,
type: type
}
var messages = res.locals.flash
// do not allow duplicate flash messages
for (var i = 0; i < messages.length; i++) {
var message = messages[i]
if (msg.type === message.type && msg.message === message.message) return this
}
messages.push(msg)
return this
}