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

What is .parent() in this video?

Since this video is about NodeJS and mongoose, not about jQuery, I searched the website but still did not find the .parent() method in the video. Can anyone explain to me what this method does? Thanks!

Patrick Mockridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Patrick Mockridge
Full Stack JavaScript Techdegree Graduate 45,611 Points

.parent() is a jQuery method that traverses the Document Object Model to the parent element. So, for example, if you had a page that was just a series of <li> elements nested directly within a <div> element, calling $("li").parent() would select the 'parent' <div> element.

For more information: http://www.w3schools.com/js/js_htmldom_navigation.asp

4 Answers

Serghei Berezovschi
Serghei Berezovschi
13,360 Points

In this particular case this.parent refers to the parent document of the document resulting from AnswerSchema. In other words the answers document is a child of the question document since it is being referenced inside of it. This.parent basically just searches for the parent of the answers document (which in our case happens to be the Question document and updates it with .save() metthod.

Dave Degeatano
Dave Degeatano
2,267 Points

How do you downvote or remove the highlighted answer on this question provided by the "graduate" who responded about the jQuery method which has nothing to do with this video on Mongoose/MongoDB... :/

It's unbelievable. I have no tolerance to such people who have no idea what are they talking about, but still write "answers". Can imagine how many people this comment can confuse.

Tom Geraghty
Tom Geraghty
24,174 Points

This question is a year old but I'm not satisfied with any of the answers provided. Can anyone point me to the documentation that shows the definition for the call to parent() in the code from this video?

It seems clear it's not jQuery since jQuery is never loaded in this set of videos. However that is definitely the top search result.

There is a deprecated listing for __parent in the MDN here which is not supported.

The API docs for Mongoose here have a few listings for parent like this:

Subdocument.prototype.parent = function() {
  return this.$parent;
};

and this:

EmbeddedDocument.prototype.parent = function() {
  return this.__parent;
};

This is confusing to me still. Both Subdocument and EmbeddedDocument reference a different syntax for parent and neither of them show where those references were defined (unless they are using the deprecated __parent from the MDN link above).

Can anyone show me the docs?? Thanks!

Antti Lylander
Antti Lylander
9,686 Points

Andrew Chalkley can you enlighten us, please?

I believe it's mongoose's method but can't find any documentation. Found something on the mongoose's files though.

I get the error this.parent is nit a function

I have to say that this is the worst documentation I've ever read of a tool with such a reputation ; ) Here is the article which mentions Model.prototype.parent function. It's not even in their API reference!