英文:
Why are messages not appearing when using connect-flash with res.locals in express.js and ejs templating?
问题
以下是您提供的代码的中文翻译:
这里是一些相关的代码片段。
connect-flash 能够在 test1 和 test2 上显示消息。
问题在于当所有三个测试重定向到相同的 "testing" 页面时,test 3 没有显示消息。
app.js
// Connect-Flash
app.use(flash());
app.use((req, res, next) => {
res.locals.errorMessage = req.flash('errorMessage');
res.locals.successMessage = req.flash('successMessage');
next();
});
auth.js
// 测试 1
router.get('/flashsucceed', function (req, res) {
req.flash('successMessage', 'Flash success message!');
res.redirect('/auth/testing');
});
// 测试 2
router.get('/flashfailed', function (req, res) {
req.flash('errorMessage', 'Flash error message!');
res.redirect('/auth/testing');
});
// ejs 文件(显示闪存消息)
router.get('/testing', function (req, res) {
res.render('testing');
});
// 测试 3:用户注册
router.post('/register', async (req, res) => {
try {
const newUser = await User.register(new User({
userFullName: req.body.userFullName,
username: req.body.username,
}), req.body.password );
req.flash('successMessage', 'Flash success message!');
passport.authenticate('local')(req, res, () => {
res.redirect('/auth/testing');
});
} catch (err) {
res.send(err);
}
});
testing.ejs
<%- include('partials/header.ejs') %>
<div class="container">
<br>
<%= successMessage %>
<%= errorMessage %>
<h3>Testing</h3>
</div>
<%- include('partials/footer.ejs') %>
如果您需要任何进一步的指导或探讨连接闪存的替代方案,请随时提出。connect-flash 应该能够在同一个 testing.ejs 文件上显示 successMessage / errorMessage。
英文:
here are some of the relevant code snippets.
connect-flash is able to show messages on test1 and test2.
the problem is that no messages is showing for test 3: user registration when all three tests are redirected to the same 'testing' page.
app.js
// Connect-Flash
app.use(flash());
app.use((req, res, next) => {
res.locals.errorMessage = req.flash('errorMessage');
res.locals.successMessage = req.flash('successMessage');
next();
});
auth.js
// test 1
router.get('/flashsucceed', function (req, res) {
req.flash('successMessage', 'Flash success message!');
res.redirect('/auth/testing');
});
// test 2
router.get('/flashfailed', function (req, res) {
req.flash('errorMessage', 'Flash error message!');
res.redirect('/auth/testing');
});
// ejs file (showing the flash messages)
router.get('/testing', function (req, res) {
res.render('testing');
});
// test 3: User Registration
router.post('/register', async (req, res) => {
try {
const newUser = await User.register(new User({
userFullName: req.body.userFullName,
username: req.body.username,
}), req.body.password );
req.flash('successMessage', 'Flash success message!');
passport.authenticate('local')(req, res, () => {
res.redirect('/auth/testing');
});
} catch (err) {
res.send(err);
}
});
testing.ejs
<%- include('partials/header.ejs') %>
<div class="container">
<br>
<%= successMessage %>
<%= errorMessage %>
<h3>Testing</h3>
</div>
<%- include('partials/footer.ejs') %>
any pointers? I would also welcome an alternative to connect-flash, if there is one.
connect-flash should show successMessage / errorMessage on the same testing.ejs file.
答案1
得分: 0
I moved the req.flash code from outside passport.authenticate to inside passport.authenticate
// 测试 3:用户注册
................
passport.authenticate('local')(req, res, () => {
req.flash('successMessage', 'Flash success message!');
res.redirect('/auth/testing');
});
} catch (err) {
res.send(err);
}
});
and the problem is solved.
maybe someone can enlighten me on why this works. thanks.
英文:
I moved the req.flash code from outside passport.authenticate to inside passport.authenticate
// test 3: User Registration
................
passport.authenticate('local')(req, res, () => {
req.flash('successMessage', 'Flash success message!');
res.redirect('/auth/testing');
});
} catch (err) {
res.send(err);
}
});
and the problem is solved.
maybe someone can enlighten me on why this works. thanks.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论