question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Reactions:2
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
chenzihuicommented, Jun 4, 2012

Encountered the same problem as well.

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

1reaction
RachelScodescommented, Jan 18, 2017

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found