Instance Methods on existing Documents

26 views
Skip to first unread message

Bernardo Figuerêdo Domingues

unread,
Jan 30, 2015, 10:55:37 AM1/30/15
to mongoo...@googlegroups.com
Hi;

I'm trying to add some instance methods to a Schema where there is already a collection with existing documents. I'm trying to do something like this:
var mySchema = new mongoose.Schema({
   foo
: Number
}, {
 collection
: 'myCollection'
});


mySchema
.methods.addFoo = function (amount) {
 console
.log('adding foo: ' + amount);
};


var myModel = mongoose.model('myModel', mySchema);

And it works fine when I do:
var myDocument = new myModel({
 foo
: 1
});


myDocument
.addFoo(1); //adding foo: 1

But, when I do this, it doesn't work:
myModel.find({_id : '123abc'}, function (err, doc) {
 doc
.addFoo(1); //TypeError: Object has no method 'addFoo'
});

Am I doing this wrong or did I misunterstand the use of instance method?

Thanks in advance;
Bernardo

Bernardo Figuerêdo Domingues

unread,
Jan 30, 2015, 11:25:44 AM1/30/15
to mongoo...@googlegroups.com
Nevermind... I've been heads deep on this for so long that I forgot that find() returns an Array and findOne returns the object.

I'm sorry for my brain fart. :)

Best Regards!

Bernardo Figuerêdo Domingues, Ph.D.


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to a topic in the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongoose-orm/FLOTuBeMzk8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages