登录用户后重新加载部分

时间:2019-04-19 03:56:35

标签: node.js partial-views

此刻我可以登录用户,但问题是部分视图标题没有重新加载,因此显示了错误的链接。我该如何解决?

'''
app.post("/login", function(req, res) {
  //Post Method will create user, check and if ok, login by using passport
  const user = new User({
    username: req.body.username,
    password: req.body.password,
  });
  req.login(user, function(err) {
    if (err) {
      console.log("Login Error " + err);
    } else {
      //make use of created cookie
      passport.authenticate("local")(req, res, function() {
        User.findOne({_id: req.user._id}, function(err, list) {
          if (err) {
            console.log(err);
          } else {
            res.render("list", {
              list: list
            });
 '''


'''
//partial view for header
<body>

  <!-- Navigation -->
  <nav class=" container navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
    <div class="container">
      <a class="navbar-brand" href="/">Training Stats</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul class="navbar-nav ml-auto">
          <% if(isAuthenticated) { %>
          <li class="nav-item" id="list"><a class="nav-link" href="/list">Reporting</a></li>
          <li class="nav-item" id="blog"><a class="nav-link" href="/blog">Journal</a></li>
          <li class="nav-item" id="compose"><a class="nav-link" href="/compose">New Entry</a></li>
          <li class="nav-item" id="logout"><a class="nav-link" href="/logout">Logout</a></li>
          <% } else { %>
          <% console.log(isAuthenticated); %>
          <li class="nav-item" id="register"><a class="nav-link" href="/register">Register</a></li>
          <li class="nav-item" id="login"><a class="nav-link" href="/login">Login</a> </li>
          <% } %>
        </ul>
      </div>
    </div>
  </nav>
'''

我很伤心。用户登录有效,但是要重新加载标题,我必须单击某个链接或通过f5等手动重新加载页面。

0 个答案:

没有答案