title: How do you iterate over Mongoose model properties within a middleware function?
body: <p>I'm attempting to iterate over fields in a mongoose model within a middleware function. The current context <code>this</code> is the model object itself. So I have a function in which the context is a Javascript object like this:</p>
  
  <pre><code>{
    lastName: 'Wilkens',
    firstName: 'Elepart',
    name: 'Elepart Wilkens',
    username: 'eK',
    bio: '&lt;script&gt;alert(\'this is a hack!!\')',
    _id: 53b17dd0e8c5af50c1d73bc6,
    language: 'en',
    country: 'USA',
    online: true
  }
  </code></pre>
  
  <p>I want to iterate over this object (which is represented in the current function with <code>this</code>). Every time I attempt to iterate with loops, it prints out values that look like internal Javascript metadata. Is it possible to iterate over <code>this</code> within a function if <code>this</code> represents an object?</p>
  
  <p>Here's the actual middleware function:</p>
  
  <pre><code>userSchema.pre('save', function (next) {
      console.log(this); // This prints precisely the same object I have copied above
  
      var fields = Object.keys(this);
  
      for(var i = 0; i &lt; fields.length; i++) {
          console.log(this[fields[i]]);
      }
  
      for(var key in this) {
        if(this.hasOwnProperty(key)) {
          console.log(this[key]);
        }
      }
  });
  </code></pre>
  
  <p>And the output is:</p>
  
  <pre><code>{ 
   strictMode: true,
   selected: undefined,
   shardval: undefined,
   saveError: undefined,
   validationError: undefined,
   adhocPaths: undefined,
   removing: undefined,
   inserting: undefined,
   version: undefined,
   getters: {},
   _id: undefined,
   populate: undefined,
   populated: undefined,
   wasPopulated: false,
   scope: undefined,
   activePaths: 
     { paths: 
        { username: 'modify',
          firstName: 'modify',
          name: 'modify',
          online: 'default',
          country: 'default',
          language: 'default',
          _id: 'default',
          bio: 'modify',
          lastName: 'modify' },
       states: { default: [Object], init: {}, modify: [Object], require: {} },
       stateNames: [ 'require', 'modify', 'init', 'default' ],
       map: [Function] },
    ownerDocument: undefined,
    fullPath: undefined }
  true
  undefined
  0
  { online: true,
    country: 'USA',
    language: 'en',
    _id: 53b1825a00ed9af7c12eedf9,
    bio: '&lt;script&gt;alert(\'this is a hack!!\')',
    username: 'yK',
    name: 'Yershay Wilkens',
    firstName: 'Yershay',
    lastName: 'Wilkens' }
  { save: 
     [ { [Function] isAsync: false },
       { [Function: checkForExistingErrors] isAsync: false },
       { [Function: validation] isAsync: false },
       { [Function] isAsync: false },
       { [Function] isAsync: false } ],
    validate: [ { [Function] isAsync: false } ] }
  { save: [], validate: [] }
  { [Function] numAsyncPres: 0 }
  { [Function] numAsyncPres: 0 }
  {}
  </code></pre>