Reputation: 474
I use mongoose random plugin.
In my schema definition i call
GameSchema.plugin(random, { path: 'r' });
After that I have a custom static method who use the plugin:
GameSchema.statics.someMethod {
[...]
GameSchema.findRandom...
And I get the error
TypeError: Object #<Schema> has no method 'findRandom'
Is there a way to achieve what I am trying to do or should I implement some kind of repository ?
EDIT:
Ben's answer worked, I needed to use findRandom on the model and not the schema.
Precision for those in my case: you need to declare first your static function
GameSchema.statics.someMethod {
[...]
Game.findRandom...
then register your schema
var Game = mongoose.model('Game', GameSchema);
otherwise you'll get "Model .... has no method 'someMethod'"
Game variable in the static function is recognized event though it is defined only later in the script.
=> bonus question: does anyone know why it works ?
Upvotes: 0
Views: 1913
Reputation: 32117
You're calling the method on the schema, whereas you need to be calling it on the model.
var Game = mongoose.model('Game', GameSchema);
Game.findRandom()...
Upvotes: 1