Hi,
The following
var mongoose = require('mongoose')
, CarSchema
, Car
mongoose.connect('mongodb://localhost/bug_or_feature');
CarSchema = new mongoose.Schema({ reg: Number });
CarSchema.methods.byReg_FAILING = function (reg, fn) {
this.find({ reg: reg }, fn);
};
CarSchema.methods.byReg = function (reg, fn) {
Car.find({ reg: reg }, fn);
};
Car = mongoose.model('Car', CarSchema);
var c = new Car();
c.byReg_FAILING('666');
c.byReg('1337');
mongoose.disconnect();
Produces
$ node mongoose-test.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Object { _id: 4f5f67d498d17ca168000001 } has no method 'find'
at model.byReg_FAILING (/home/torgeir/code/mongoose-test.js:9:8)
at Object.<anonymous> (/home/torgeir/code/mongoose-test.js:17:3)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)
Reading docs and the example my impression was that both of CarSchema's instance methods should be working?
Changing this.find
for this.db.model('Car').find
, however, seems to be working.
Regards,
Torgeir
Thank you so much for this! Saved me a lot of time and headache. Would be great if the documentation was updated to reflect this.
(Eller for 氓 si det p氓 norsk; takk!)
Ditto. This had me confused for a long time. Is this a bug or a documentation problem?
Encountered the same problem as well.
Seems to work when you just use this.model('Car').find
as well.
Thankyou! Same problem for me.
Thanks! I was having the same issue.
Same here! thank you!
+1
+1
+1
Can you explain what error you're seeing? instances should NOT have the .find
property on them. That is a schema-level method. This functionality is intentional.
@varunjayaraman
instances should NOT have the .find property on them. That is a schema-level method. This functionality is intentional.
This ticket was opened a while ago. As of the last time I checked, they now do correctly show that when using find
on an instance method it should be invoked as this.Model.find
, aka calling find on the class, not the instance, which makes total sense.
Thanks @varunjayaraman
@RachelScodes
it should be invoked as this.Model.find
it actually should be this.model('Car').find()
according to the documentation.
Most helpful comment
Encountered the same problem as well.
Seems to work when you just use
this.model('Car').find
as well.