Mongoose - add method to object that is returned in the callback Mongoose - add method to object that is returned in the callback mongoose mongoose

Mongoose - add method to object that is returned in the callback


The Schema.method description, from the docs:

Adds an instance method to documents constructed from Models compiled from this schema.

So, if you do something like this:

var userSchema = new Schema({    username: String,    age: Number});userSchema.method('showAge', function () {    return this.age;});

, and call your method in a document returned from a query like:

User.findOne({'age':'20'}, function(err, user){    console.log(user.showAge());});

it should work. Maybe you're having problems because you're calling your method users.function() in an array. Remember: the find method returns an array of documents.