title: Updating a user record in Node js + Express + Passport + MongoDB
body: <p>Ok, so I've been struggling with this for hours and can't figure out how to get it to work.</p>
  
  <p>I followed this tutorial to completion on setting up nodejs with mongodb, express and passport:
  <a href="https://scotch.io/tutorials/easy-node-authentication-setup-and-local" rel="nofollow">https://scotch.io/tutorials/easy-node-authentication-setup-and-local</a>
  It works great, except now I want to use the authenticated session to allow the user to create a display name via a form.</p>
  
  <p>I'm stumped because I can't figure out how to use the route/passport/session method to update the user record in mongodb via a POST request.</p>
  
  <p>My question relates to the 'Process Update Profile' block of code below. I'm trying to use the user.update function to add a displayname to my user record. I get the following error:</p>
  
  <pre><code>ReferenceError: user is not defined
  at Object.handle (/vagrant/new/app/routes.js:65:9)
  at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:103:13)
  at Object.isLoggedIn [as handle] (/vagrant/new/app/routes.js:115:16)
  at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:103:13)
  at Route.dispatch (/vagrant/new/node_modules/express/lib/router/route.js:107:5)
  at /vagrant/new/node_modules/express/lib/router/index.js:195:24
  at Function.proto.process_params (/vagrant/new/node_modules/express/lib/router/index.js:251:12)
  at next (/vagrant/new/node_modules/express/lib/router/index.js:189:19)
  at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:77:14)
  at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:81:14)
  at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:81:14)
  at Route.dispatch (/vagrant/new/node_modules/express/lib/router/route.js:107:5)
  at /vagrant/new/node_modules/express/lib/router/index.js:195:24
  at Function.proto.process_params (/vagrant/new/node_modules/express/lib/router/index.js:251:12)
  at next (/vagrant/new/node_modules/express/lib/router/index.js:189:19)
  at next (/vagrant/new/node_modules/express/lib/router/index.js:166:38)
  </code></pre>
  
  <p>I can see that user isn't passed to the function I'm using, but I can't figure out what the correct method should be - or if I'm approaching this the right way at all.
  See code below:</p>
  
  <pre><code>// app/routes.js
  module.exports = function(app, passport) { 
  // =====================================
  // HOME PAGE (with login links) ========
  // =====================================
  app.get('/', function(req, res) {
      res.render('index.ejs'); // load the index.ejs file
  });
  
  //... some code (login, signup methods) omitted for brevity ...
  
  // =====================================
  // UPDATE PROFILE =================
  // =====================================
  app.get('/updateprofile', isLoggedIn, function(req, res) {
      res.render('updateprofile.ejs', {
          user : req.user // get the user out of session and pass to template
      });
  });
  
  // =====================================
  // PROCESS UPDATE PROFILE=======================
  // =====================================
  // process the update profile form
  app.post('/updateprofile', isLoggedIn, function(req, res) {
      user.update({_id: req.session.passport.user}, {
          displayName: req.body.displayName 
      }, function(err, numberAffected, rawResponse) {
         console.log('new profile update error');
      });
      res.render('profile.ejs', {
          user : req.user // get the user out of session and pass to template
      });
  });
  
  // =====================================
  // PROFILE SECTION =====================
  // =====================================
  // we will want this protected so you have to be logged in to visit
  // we will use route middleware to verify this (the isLoggedIn function)
  app.get('/profile', isLoggedIn, function(req, res) {
      res.render('profile.ejs', {
          user : req.user // get the user out of session and pass to template
      });
  });
  </code></pre>
  
  <p>};
  function isLoggedIn(req, res, next) {</p>
  
  <pre><code>// if user is authenticated in the session, carry on 
  if (req.isAuthenticated())
      return next();
  
  // if they aren't redirect them to the home page
  res.redirect('/');
  </code></pre>
  
  <p>}</p>