Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you iterate over Mongoose model properties within a middleware function?

I'm attempting to iterate over fields in a mongoose model within a middleware function. The current context this is the model object itself. So I have a function in which the context is a Javascript object like this:

{
  lastName: 'Wilkens',
  firstName: 'Elepart',
  name: 'Elepart Wilkens',
  username: 'eK',
  bio: '<script>alert(\'this is a hack!!\')',
  _id: 53b17dd0e8c5af50c1d73bc6,
  language: 'en',
  country: 'USA',
  online: true
}

I want to iterate over this object (which is represented in the current function with this). Every time I attempt to iterate with loops, it prints out values that look like internal Javascript metadata. Is it possible to iterate over this within a function if this represents an object?

Here's the actual middleware function:

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 < fields.length; i++) {
        console.log(this[fields[i]]);
    }

    for(var key in this) {
      if(this.hasOwnProperty(key)) {
        console.log(this[key]);
      }
    }
});

And the output is:

{ 
 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: '<script>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 }
{}
like image 219
Glen Selle Avatar asked Jun 30 '14 15:06

Glen Selle


1 Answers

You're using integer indexes instead of the string references from the fields array. It should be:

var fields = Object.keys(this);

for(var i = 0; i < fields.length; i++) {
    console.log(this[fields[i]]);
}

(e.g., you were doing this[1], this[2], instead of this[fields[1]])

like image 192
psantiago Avatar answered Nov 15 '22 03:11

psantiago