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 User Authentication With Express and Mongo Sessions and Cookies Authenticating the Username and Password

Myron Castillo
Myron Castillo
12,916 Points

How is the 'user' in 'user._id' established as being the holder of all of the user information? Where was that set up?

In the video, when discussing the req.session.userId, I understand that we're making the session and setting a property called "userId". But he sets the value equal to 'user._id' and says that "user" represents the user data. I'm confused about this part because I want to understand where that was set up. Is it being accessed from the database? I'm not entirely sure. Please help.

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

Well the user is a model that was created to represent an actual user within mongodb. Remember, the user model that you created in a much earlier lesson? Well that is the model that mongodb uses to save user data, such as the id, username, password, etc. It is just that in order to access/create that information for/from mongodb we created an instance of the model (just a javascript object) called user that holds all of the information....do you better understand now, or do you need more explaining?

Here are some links about models:

data-model-design

models in mongoose

Myron Castillo
Myron Castillo
12,916 Points

Thank you for responding. When you say the user model that was created, are you referring to the schema in user.js? The one exported as User?

Dane Parchment
Dane Parchment
Treehouse Moderator 11,075 Points

To your first question yes, I am referring to the Schema you created (I should have referred to it as a Schema instead of a model, but at least you get it now)!

Myron Castillo
Myron Castillo
12,916 Points

I figured it out thanks to you. I was confused because I had exported the model as User and I was expecting it to be User._id. I was even confused as to why it was called users in my Mongo Database. After some experimentation, it looks like Mongo takes User and makes it into users. It also seems that referring to it doesn't require me to use 'User', but as you said an instance of it can be referred to as "user". Thanks a lot!

Dane Parchment
Dane Parchment
Treehouse Moderator 11,075 Points

Great job! Experimentation is the best way to learn a new programming language/concept and the fact that you are using your own intuition along with help from others tells me that you are on your way to excelling in this new JavaScript environment. Also....love your profile picture!