Storing class objects in a session (expressjs)

2,210 views
Skip to first unread message

ec.developer

unread,
Jul 4, 2012, 5:35:12 PM7/4/12
to nod...@googlegroups.com
Hi all,
I'm trying to store a class object in a session (I actually use ExpressJS). For class properties everything is fine, but class methods are missed, when I access the object. 
So for example, I have a class:

var SomeClass = module.exports = function() {
     this.param1 = "1";
     this.param2 = "2";

     this.param3 = function(){ return "3"; };
}

And I instantiate it directly to a session:
req.session.myobj = new SomeClass();

When the new request is made, I'm trying to access the object req.session.myobj, and I can access only param1 and param2; param3 is unreachable (it is not defined). 

Is there any way to solve it? 

Thanks.

mscdex

unread,
Jul 4, 2012, 6:05:49 PM7/4/12
to nodejs
On Jul 4, 5:35 pm, "ec.developer" <ec.develo...@gmail.com> wrote:
> Hi all,
> I'm trying to store a class object in a session (I actually use ExpressJS).
> For class properties everything is fine, but class methods are missed, when
> I access the object.

My guess (without researching) is that the object is being serialized
JSON, which does not allow for functions.

If my guess is correct, you'd have to convert functions to strings
during the serialization process (JSON.stringify accepts a callback as
a second argument where you can do this easily) and know to
deserialize those particular property values as functions instead of
ordinary strings.

Nathan Rajlich

unread,
Jul 4, 2012, 6:10:32 PM7/4/12
to nod...@googlegroups.com
Indeed Express does serialize the session object behind the scenes, to support external data stores like Redis, etc.

The solution is to not put functions there. Define them somewhere else, pass them the needed arguments (even the entire session object if you want). Only put serializable data into the session object (Strings, Numbers, Objects, Arrays, and null are about it).


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

ec.developer

unread,
Jul 5, 2012, 4:00:20 AM7/5/12
to nod...@googlegroups.com
Thanks for advices =) I think I'll store in session just the static data, and object that I need to use will create in other place. 

dolphin 278

unread,
Jul 5, 2012, 6:04:46 AM7/5/12
to nod...@googlegroups.com
You could also inherit your own implementation of session store based on existing one, and alter load method to add your function when session data loads into memory.
---------
Boris Egorov
skype/gtalk/nickname: dolphin278
mobile: +7 905 728 1543


--

tjholowaychuk

unread,
Jul 6, 2012, 12:45:54 AM7/6/12
to nodejs
this is by-design, you should only really store JSON-able things, you
could store more
if you're backing with mongo or have some special store written your
codec but yeah typically not recommended,
there's really little incentive to do that sort of thing anyway
Reply all
Reply to author
Forward
0 new messages