Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assigning mongoose documents to the session causes it to die a violent death #17

Closed
taxilian opened this issue Sep 4, 2015 · 8 comments

Comments

@taxilian
Copy link

taxilian commented Sep 4, 2015

In a few (rare) cases I need to just assign a mongoose document to the session for quick/easy lookup. I realize it's probably not a wonderful idea, and that I should probably store and id and load it, etc etc etc. Granted.

However, in connect-mongo this worked great; in connect-mongodb-session I get a maximum call stack exceeded and it all explodes in a giant burning fireball of death. What do you think of the idea of having .toObject applied to any object assigned to the session which supports that method? Alternatively one could JSON.stringify and then JSON.parse before saving, which would be extremely effective at making sure it's serializable, but... well, pros and cons to that too.

Thoughts?

@vkarpov15
Copy link
Member

What does the code you're using to assign a mongoose doc look like?

@taxilian
Copy link
Author

taxilian commented Sep 4, 2015

req.session.superuser = superuser;

(superuser is a mongoose document)

@ghost
Copy link

ghost commented Jan 28, 2016

Sorry for hijacking the thread, but I am also receiving errors when storing objects in the session record that were assigned from Mongoose documents. If relevant, the problem does not occur if I first translate objects using Document.toObject() from Mongoose, which converts an in-memory document object model to a plain JavaScript Object instance. And, I understand that this might be necessary.

What I don't understand is what Mongoose could be adding to an object's fields such that assigning them directly to the session as fields is then later causing this otherwise obscure error.

For this case, the "response" object has the following composition:

'response': {
  'user': {
    'id': user._id.toString(),
    'created': user.created.toString(),
    'isActive': user.isActive,
    'lastLogin': (user.lastLogin === null) ? null : user.lastLogin.toString(),
    'displayName': user.displayName.toString(),
    'privileges': user.privileges
  }
}

req.session.user = response.user; //<< the "response" object above and its "user"

I then execute a manual save as I have express-session "resave" turned off:

req.session.save(function (err) {
  // see below for contents of err
});

At the completion callback, err is set as follows:

error: mongo session store error [Error: Error setting 51519507-1045-4554-be07-8c2c73dabcfc to Session {
  cookie: 
   { path: undefined,
     _expires: undefined,
     originalMaxAge: undefined,
     httpOnly: undefined,
     secure: undefined },
  user: 
   { id: '56aa5255f123f1807944ed74',
     created: 'Thu Jan 28 2016 12:39:33 GMT-0500 (EST)',
     isActive: true,
     lastLogin: 'Thu Jan 28 2016 12:41:03 GMT-0500 (EST)',
     displayName: 'rob',
     privileges: { rank: 'user', level: 1 } } }: Maximum call stack size exceeded]

@vkarpov15
Copy link
Member

A mongoose document is a very complex object. For instance, if you do Object.keys() on a mongoose doc, you get:

  [ '$__',
  'isNew',
  'errors',
  '_doc',
  '$__original_save',
  'save',
  '_pres',
  '_posts' ]

Would not recommend saving this to mongodb directly. Just use toObject().

@r3wt
Copy link

r3wt commented Jan 10, 2017

@vkarpov perhaps add a note about this in the documentation.

@vkarpov15
Copy link
Member

@r3wt I think this should actually be fixed with newer versions of mongoose (try >= 4.7.7 re Automattic/mongoose#4866). If it still doesn't work, please provide a code sample that demonstrates your issue.

@r3wt
Copy link

r3wt commented Jan 18, 2017

@vkarpov15 well i came here from google, and the version of mongoose i am running is 4.7.6, i'll try 4.7.7 and let you know if it works. But i decided against storing the object anyway, instead just storing the user id.

@vkarpov15
Copy link
Member

yeah that's a good idea. Let me know if the new mongoose version fixes it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants