Is there a way to add functions to the object that is returned in the callback?
User.find({'age':'20'}, function(err, users){
users.function();
});
It seems statics only work on the model. Schematically :
User.static();
and methods only work on instances
(new User()).method();
None of them seem to work on users, which i think is just a normal js Object variable. Am I missing something ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With