Schema instance methods referencing this.find, this.findOnce, etc. throws
See original GitHub issueHi,
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
Issue Analytics
- State:
- Created 12 years ago
- Reactions:2
- Comments:13 (1 by maintainers)
Top Results From Across the Web
Mongoose v6.8.2: Schemas
Defining your schema; Creating a model; Ids; Instance methods; Statics ... schema); const doc = new Model(); await doc.save(); // Throws "document must...
Read more >Mongoose Schema Instance Methods is Not a Function
For testing purposes, I've tried to hard code a UserModel.findOne() right into connection field and I do get a user back. I then...
Read more >Adding Data to MongoDB (Example) | Treehouse Community
Hello, I'm attempting to add data to the nested array of a mongo document. My 'UserSchema' is holding the array for my ProductSchema....
Read more >1. Working with Spring Data Repositories
In this first step you defined a common base interface for all your domain repositories and exposed findOne(…) as well as save(…) .These...
Read more >Collections and Schemas - Meteor Guide
After reading this guide, you'll know: The different types of MongoDB collections in Meteor, and how to use them. How to define a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Encountered the same problem as well.
Seems to work when you just use
this.model('Car').find
as well.@varunjayaraman
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 asthis.Model.find
, aka calling find on the class, not the instance, which makes total sense.Docs: http://mongoosejs.com/docs/guide.html#methods