Render updated document to ejs page mongoose node js

I have an app where users can create an account and update their information after creation. I am able to save the updated document to the database when users input new information on the edit page, however, when I redirect them to the user panel, the old information is still being displayed. I believe that it has something to do with the session that gets created on login because the updated information only shows once the user logs out and logs back in. This is my edit page:

<%- include('partials/header') %>

<% if(user.firstName.endsWith("s")) { %>
<h1 class="dashboard-title"><%=user.firstName + "' Account"%></h1>
<% } else { %>
<h1 class="dashboard-title"><%=user.firstName + "'s Account"%></h1>
<% } %>

<!-- Action Buttons -->
<div class="container">
  <div class="row justify-content-center">
    <div class="col-md-3">
      <a href="/logout" class="dashboard-btn">
        <h5>Logout <i class="fas fa-door-open"></i></h5>
      </a>
    </div>
    <div class="col-md-3">
      <a href="/edit" class="dashboard-btn">
        <h5>Edit <i class="fas fa-edit"></i></h5>
      </a>
    </div>
  </div>
</div>

<br>

<!-- Information -->
<div class="container">
  <form class="" action="/edit" method="post">
    <div class="row justify-content-center">
      <label class="col-form-label col-md-1" for="firstName">First Name:</label>
      <div class="form-group col-md-7">
        <input name="firstName" class="form-control" type="text" value="<%=user.firstName%>" >
      </div>
    </div>
    <br>
    <div class="row justify-content-center">
      <label class="col-form-label col-md-1" for="lastName">Last Name:</label>
      <div class="form-group col-md-7">
        <input name="lastName" class="form-control" type="text" value="<%=user.lastName%>" >
      </div>
    </div>
    <br>
    <div class="row justify-content-center">
      <label class="col-form-label col-md-1" for="email">Email:</label>
      <div class="form-group col-md-7">
        <input name="email" class="form-control" type="text" value="<%=user.username%>" readonly>
      </div>
    </div>
    <br>
    <div class="row justify-content-center">
      <label class="col-form-label col-md-1" for="phone">Phone:</label>
      <div class="form-group col-md-7">
        <input name="phone" class="form-control" type="text" value="<%=user.phoneNumber%>" >
      </div>
    </div>
    <br>
    <div class="row justify-content-center">
      <label class="col-form-label col-md-1" for="address">Address:</label>
      <div class="form-group col-md-7">
        <input name="address" class="form-control" type="text" value="<%=user.personalInfo.address%>" >
      </div>
    </div>
    <br>
    <div class="row justify-content-center">
      <label class="col-form-label col-md-1" for="coverage">Coverage:</label>
      <div class="form-group col-md-7">
        <input name="coverage" class="form-control" type="text" value="<%=user.coverage%>" >
      </div>
    </div>
    <br>
    <div class="row justify-content-center">
      <label class="col-form-label col-md-1" for="paymentPlan">Payment:</label>
      <div class="form-group col-md-7">
        <input name="paymentPlan" class="form-control" type="text" value="<%=user.paymentPlan%>">
      </div>
    </div>
    <br>
    <button class="btn register-btn" type="submit">Submit Changes</button>
  </form>
</div>

<%- include('partials/footer') %>

And app.js to handle the post requests to edit route:

app.get("/user-panel", function(req, res) {
    if(req.isAuthenticated()) {
      res.render("user-panel", {
        user: req.user
      });
    } else {
      res.redirect('/login');
    }
  });

  app.get("/edit", function(req, res) {
    if(req.isAuthenticated()) {
      res.render("edit", {
        user: req.user
      });
    } else {
      res.redirect("/login");
    }
  });

  app.post("/edit", function(req, res) {
    const email = req.user.username;
    const firstName = req.body.firstName;
    const lastName = req.body.lastName;
    const phone = req.body.phone;

    User.findOne({username: email}, function(err, foundUser) {
      if(err) {
        console.log(err);
      } else {
        if (foundUser) {
          foundUser.firstName = firstName;
          foundUser.save();
          res.redirect("/user-panel");
        }
      }
    });
  });

Any ideas on how I can get the server to respond with the updated info without having to log out the user?


I want to touch on a few things.

  1. to answer your question, you need to query the database again upon redirect. like so:

you need to pass the email value back to the original user-panel function:

res.redirect("/user-panel?username=" + email);

then, you have to query the database again, like so:

    app.get("/user-panel", authCheck, function(req, res) {
    let email = req.query.email

    User.findOne({username: email}, function(err, foundUser) {
        if(err) {
            console.log(err);
        } else {
            if (foundUser) {
            foundUser.firstName = firstName;
            foundUser.save();
            res.render("user-panel", {
                user: req.user
            });
            }
        }
    });
        
        
  });
  1. You should try not to use authentication logic within each function, rather have it as a seperate function and import it as middleware. Makes it cleaner. I've rewritten it for you.

    function authCheck(req, res, next) {
        if(req.isAuthenticated()) {
            next()
        } else {
            res.redirect('/login');
        }
    
    }

now within any new function you create (within this file), you can just use the function authCheck() as middleware:

app.get('/', authCheck, (req, res) => {