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 Communicating with Mongo through Mongoose Extending Mongoose

Why we will get an error when declare `AnswerSchema.methods.update()` after QuestionSchema?

Hi treehousers, I have 2 questions in understanding this video:

First: At about 8:50 Andrew said we should declare AnswerSchema.methods.update() after QuestionSchema, else we will get an error. For the reason that declaring schema before question Schema can make AnswerSchema a sub-schema of Question schema.

But why? does the order of declaring schemas matter in Mongoose? I mean following the logic of our teacher, if we declare Schema A then declare Schema B in Mongoose, then Schema A becomes automatically a child schema of Schema B? It does not make sense to me, since in this case as far as I understand, its this line of code answers: [AnswerSchema] inside the QuestionSchema which makes AnswerSchema a child of QuestionSchema.

Does the order of declaring schemas really matter, and how?

Second: When we write AnswerSchema.method("update", () => {}) we create a method of the instance. Is there a reason that we don't use instead AnswerSchema.update(() => {}) as this will create a method update and works the same?

Could someone explain to me?

Thanks in advance.

1 Answer

Hi Ran, for your first question about the order of declaring schemas in Mongoose - the reason that AnswerSchema is declared first is because the AnswerSchema is used as a child of QuestionSchema as an array of answers:

var QuestionSchema = new Schema({
    text: String,
    createdAt: {type: Date, default: Date.now},
    answers: [AnswerSchema]
)};

In this case, since both the AnswerSchema and the QuestionSchema are declared in a single file, the AnswerSchema needs to be declared first. The AnswerSchema does not automatically become a child of QuestionSchema. Another option could have been to create two separate files, one AnswerSchema (AnswerSchema.js) and one for QuestionSchema (QuestionSchema.js). In this case, you could export the AnswerSchema by using:

var AnswerSchema= new Schema({
    //properties
)};
module.exports = AnswerSchema;

The order would not matter in this case, since each schema would be in a separate file.

For your second question, the syntax for declaring custom methods for MongoDB schemas is the following:

SchemaName.methods.methodName = function() {
    //body
}

It is incorrect to try to declare a custom method like this:

SchemaName.methodName() {

}