Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a REST API With Express Modeling Data for the API Getting More from Mongoose

Daniel Sousa
Daniel Sousa
11,975 Points

static methods

Why do you have to pass a parameter to this.model() in the static method? In this context, if this refers to the document, doesn't the each document only have one model? So why would you need to specify the model?

1 Answer

nico dev
nico dev
20,364 Points

Hi Daniel Sousa,

I will play my two cents on this one, but honestly I am not sure, and I'd love to be corrected if I'm wrong and/or if I am missing some key aspects.

I think that, although you're right in the facts you commented about singularity of model-documents relationship, the find() method here:

    AnimalSchema.methods.findSameColor = function(callback) {
        return this.model('Animal').find({color: this.color}, callback)  // --> the find() at the left, in this line.
    }

has to be called from the model, not from the document. I might have heavy front-end influence here, and maybe I am just recalling way too much the DOM traversal thing |o| but that's at least how it sounds to me.

Would love to be enlightened if I'm wrong, though, as I am just speculating.

Looking forward to reading more insightful and experienced comments and learning from them.

Tom Geraghty
Tom Geraghty
24,174 Points

You got it. Check out the docs at: http://mongoosejs.com/docs/queries.html

Of interest: "Documents can be retrieved through several static helper methods of models."

Since you call the method on the model, you gotta get the model not the document of that model's type.