Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:16:51+00:00 2026-06-02T08:16:51+00:00

If I store an object in session like this: user.name = Kelvin; // user

  • 0

If I store an object in session like this:

user.name = "Kelvin"; // user is an object pass by "mongoose" findOne's callback.
req.session.user = user; 
console.log(req.session.user.name); // Kelvin

and after that, I access “user” in other routes of express:

app.get("/somepath", function(req, resp) {
    console.log(req.session.user.name); // undefined
});

I want to know why req.session.user.name is undefined besides the function I set it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:16:53+00:00Added an answer on June 2, 2026 at 8:16 am

    After looking into mongoose source code I can say that this is because how mongoose models and session works. When you call req.session.user = user then req.session.user points to the object but actually the data needs to be stored somewhere (like memory or Redis).

    In order to do that Express calls JSON.stringify(sess) and string is stored in memory. Here’s where mongoose enters. Models are constructed in such a way, that when you stringify them only attributes predefined in Schema are included. So when you set req.session.user = user Express stringifies user (you loose name attribute) and saves the data. When you call req.session.user in another route Express parses stringified data and you obtain object without name attribute.

    Now how to fix this? There are several ways.

    • Add name attribute to the Schema;
    • Define new literal object for user:

      var newuser = { id: user.id, name : "Kelvin", pwd: user.pwd, etc. };
      req.session.user = newuser;

    • Store name in another field: req.session.name = "Kelvin"; (the best solution imho)

    By the way: You shouldn’t hold the user object in session. What if some other user like administrator makes changes to the user object? You won’t see them at all. I advice holding only the id of the user in session and make custom middleware to load user from DB and store it in request object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store something in the Session object (i.e. a user ID), before
I have currently login User object (userId,organisationId,etc.. ) stored in session by using like
I want to use session object in my web app.I want to store some
I have a list of objects which I store in the session. This list
I have used this code to store Object to a file: try{ FileOutputStream saveFile=new
I need to store this object into the internal storage memory of the phone,
I would like to store a cookie object with multiple values associated with various
Currently, we serialize user's session object into database but the object changed too much
I would like to handle the case where a user is editing an object
As part of an optimization, we don't store our entire User in the session,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.