Add arguments in a virtual getter Add arguments in a virtual getter mongoose mongoose

Add arguments in a virtual getter


Add it as an instance method instead of a virtual getter.

schema.methods.getSomething = function(what) {    if (!what) {        return this.somethingElse    } else {        return this.something[what]    }};


Getters don't accept any arguments, because they are supposed to replace normal "get attribute" functionality, without brackets. So what you are need is to define a method:

Schema.methods.getSomething = function(what) {    if (!what) {        return this.somethingElse;    } else {        return this.something[what];    }};

and then you can simply call:

mySchemaObject.getSomething( "test" );