Why are my Mongoose 3.8.7 schema getters and setters being ignored? Why are my Mongoose 3.8.7 schema getters and setters being ignored? mongoose mongoose

Why are my Mongoose 3.8.7 schema getters and setters being ignored?


I was having the same problem with getters not modifying the returned documents when querying with Mongoose. To make this apply to every query, you can do this:

// Enable Mongoose getter functionsschema.set('toObject', { getters: true });schema.set('toJSON', { getters: true });


Are you assuming virtuals are not working because they don't show up in your console.log output? If so, that is by design. Virtuals are external to your actual document so do not get printed with console.log by default. To get them to display, read these docs: http://mongoosejs.com/docs/api.html#document_Document-toObject


Try

schema.virtual('password').get(function () {    return this.username;});

as your getter function, this is your entity instance and the value parameter doesn't mean much here.

If you were writing a setter function, you would have to write this.username = value.