Why are messages not appearing when using connect-flash with res.locals in express.js and ejs templating?

huangapple go评论56阅读模式
英文:

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) =&gt; {
  res.locals.errorMessage = req.flash(&#39;errorMessage&#39;);
  res.locals.successMessage = req.flash(&#39;successMessage&#39;);
  next();
});

auth.js

// test 1 
router.get(&#39;/flashsucceed&#39;, function (req, res) {
  req.flash(&#39;successMessage&#39;, &#39;Flash success message!&#39;);
  res.redirect(&#39;/auth/testing&#39;);
});

// test 2
router.get(&#39;/flashfailed&#39;, function (req, res) {
  req.flash(&#39;errorMessage&#39;, &#39;Flash error message!&#39;);
  res.redirect(&#39;/auth/testing&#39;);
});

// ejs file (showing the flash messages)
router.get(&#39;/testing&#39;, function (req, res) {
  res.render(&#39;testing&#39;);
});

// test 3:  User Registration
router.post(&#39;/register&#39;, async (req, res) =&gt; {
  try {
      const newUser = await User.register(new User({
        userFullName: req.body.userFullName,
        username: req.body.username,
      }), req.body.password );

      req.flash(&#39;successMessage&#39;, &#39;Flash success message!&#39;);

      passport.authenticate(&#39;local&#39;)(req, res, () =&gt; {
        res.redirect(&#39;/auth/testing&#39;);
      });
  } catch (err) {
      res.send(err);
  }
});

testing.ejs

&lt;%- include(&#39;partials/header.ejs&#39;) %&gt;

&lt;div class=&quot;container&quot;&gt;
  &lt;br&gt;

  &lt;%= successMessage %&gt;
  &lt;%= errorMessage %&gt;

  &lt;h3&gt;Testing&lt;/h3&gt;
&lt;/div&gt;

&lt;%- include(&#39;partials/footer.ejs&#39;) %&gt;

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(&#39;local&#39;)(req, res, () =&gt; {
        req.flash(&#39;successMessage&#39;, &#39;Flash success message!&#39;);
        res.redirect(&#39;/auth/testing&#39;);
      });
  } catch (err) {
      res.send(err);
  }
});

and the problem is solved.

maybe someone can enlighten me on why this works. thanks.

huangapple
  • 本文由 发表于 2023年5月29日 20:13:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定