foobarbecue
foobarbecue

Reputation: 7060

What variable types can / should I use as meteor.js Session variables?

I've been doing Session.set('currentlyActiveDocument',mydocument._id) but this means I end up doing MyCollection.findOne(Session.get('currentlyActiveDocument')) very often. I wondered if it would be ok to just do Session.set('currentlyActiveDocument',mydocument) and save me those extra db lookups. That is, use an object itself (document, in this case) as a session variable rather than its ID. I don't care if reactively dependant functions are re-run when I change properties of mydocument.

I actually tried this recently and encountered the surprising problem that a method of the object (mydocument, except it wasn't a document in that case, just a JS object) did not show up on the other end when I did foo=Session.get(myobject). foo.myMethod() returned undefined.

Upvotes: 1

Views: 480

Answers (1)

Tarang
Tarang

Reputation: 75945

You can use any type of object that EJSON can encode. To see more details on this see http://docs.meteor.com/#ejson.

You could also add your own EJSON extensions for custom types.

The reason your JS object doesn't (fully) store in a Session hash is that it contains a function, the functions are ignored by EJSON.

Upvotes: 3

Related Questions