express connect-flash displays only after refresh not on the samepage.how解决这个问题?
Express connect-flash displays only after refresh not on the same page.how to solve this?
below is register route
router.post("/register", function(req, res){
var newUser= new User({username:req.body.username});
User.register(newUser,req.body.password, function(error,user){
if(error){
req.flash("error", error.message);
return res.render("register.ejs");
}else{
passport.authenticate("local")(req, res ,function(){
req.flash("success","Welcome to PhotoDiary, " + user.username);
res.redirect("/diary");
});
}
});
});
below is passport-local
app.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
i put flash-connect in register route req.flash("error", error.message);
when i sign up it shows the message after another refresh,not on the same page
使用 res.locals
将错误发送到视图不是一个好习惯,假设您正在使用 EJS
或 Pug
您也可以将消息和其他数据发送到视图文件这样。
router.post("/register", function(req, res){
var newUser= new User({username:req.body.username});
User.register(newUser,req.body.password, function(error,user){
if(error){
req.flash("error", "An Error Has Occured");
var data = {
page_title : 'Register', // Set Page Title Dynamically
message : req.flash('error'), // Set Message
};
return res.render("register.ejs" , data);
}else{
passport.authenticate("local")(req, res ,function(){
req.flash("success","Welcome to PhotoDiary");
var data = {
page_title : 'Register', // Set Page Title Dynamically
username : user.username,
message : req.flash('success'), // Set Message
};
res.redirect("/diary", data);
});
}
});
});
您将可以使用变量以这种方式访问视图文件中的数据(EJS 示例)
<Head>
<title><%= page_title %></title>
</head>
<div class="alert alert-success">
<%= message %> <%= username %>
</div>
<h1> Welcome </h1>
below is register route
router.post("/register", function(req, res){
var newUser= new User({username:req.body.username});
User.register(newUser,req.body.password, function(error,user){
if(error){
req.flash("error", error.message);
return res.render("register.ejs");
}else{
passport.authenticate("local")(req, res ,function(){
req.flash("success","Welcome to PhotoDiary, " + user.username);
res.redirect("/diary");
});
}
});
});
below is passport-local
app.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
i put flash-connect in register route req.flash("error", error.message); when i sign up it shows the message after another refresh,not on the same page
使用 res.locals
将错误发送到视图不是一个好习惯,假设您正在使用 EJS
或 Pug
您也可以将消息和其他数据发送到视图文件这样。
router.post("/register", function(req, res){
var newUser= new User({username:req.body.username});
User.register(newUser,req.body.password, function(error,user){
if(error){
req.flash("error", "An Error Has Occured");
var data = {
page_title : 'Register', // Set Page Title Dynamically
message : req.flash('error'), // Set Message
};
return res.render("register.ejs" , data);
}else{
passport.authenticate("local")(req, res ,function(){
req.flash("success","Welcome to PhotoDiary");
var data = {
page_title : 'Register', // Set Page Title Dynamically
username : user.username,
message : req.flash('success'), // Set Message
};
res.redirect("/diary", data);
});
}
});
});
您将可以使用变量以这种方式访问视图文件中的数据(EJS 示例)
<Head>
<title><%= page_title %></title>
</head>
<div class="alert alert-success">
<%= message %> <%= username %>
</div>
<h1> Welcome </h1>