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

King Penson II
King Penson II
16,413 Points

How to call the Session id in express-session

Hello,

I'm having a hard time grasping the concept of express-session and session IDs. What I want to do is use the user's ID from the session ID to post to a mongoose schema model. So that every time a new object is made in MongoDB, it'll have an ID number to reference for search queries of the user that created the product:

var ProductSchema = new mongoose.Schema({
  title: "Black Dress shoes",
  userID: "561FADSFQEFQF1961"
})

I tried setting the ID to a variable, but I can't call it to page to check if it's working, no luck:

var userId = req.sessionID;

1 Answer

Assuming you have express session set up correctly for your needs...

var session = req.session; // creates a new session

var sessionID = session.id // session is an object that has a property called 'id' with a unique session id in it