mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Schema instance methods referencing this.find, this.findOnce, etc. throws

Open torgeir opened this issue 14 years ago • 13 comments

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

torgeir avatar Mar 13 '12 15:03 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!)

joakim avatar May 01 '12 22:05 joakim

Ditto. This had me confused for a long time. Is this a bug or a documentation problem?

blumysden avatar May 15 '12 02:05 blumysden

Encountered the same problem as well.

Seems to work when you just use this.model('Car').find as well.

chenzihui avatar Jun 04 '12 09:06 chenzihui

Thankyou! Same problem for me.

jesse1981 avatar Apr 28 '15 13:04 jesse1981

Thanks! I was having the same issue.

manasb-uoe avatar Sep 16 '15 05:09 manasb-uoe

Same here! thank you!

RachelScodes avatar Nov 28 '15 20:11 RachelScodes

+1

nicholaswmin avatar Aug 18 '16 02:08 nicholaswmin

+1

codingmaven avatar Oct 31 '16 04:10 codingmaven

+1

MustafaDal avatar Jan 12 '17 16:01 MustafaDal

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.

sobafuchs avatar Jan 12 '17 16:01 sobafuchs

@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.

Docs: http://mongoosejs.com/docs/guide.html#methods

RachelScodes avatar Jan 18 '17 23:01 RachelScodes

Thanks @varunjayaraman

mernxl avatar Dec 25 '17 15:12 mernxl

@RachelScodes

it should be invoked as this.Model.find

it actually should be this.model('Car').find() according to the documentation.

luchotess avatar Apr 22 '19 05:04 luchotess