Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete or remove Session variables?

Tags:

meteor

Meteor has a Session that provides a global object on the client that you can use to store an arbitrary set of key-value pairs. Use it to store things like the currently selected item in a list.

It supports Session.set, Session.get and Session.equals.

How do I delete a Session name, value pair? I can't find a Session.delete(name) ?

like image 862
Steeve Cannon Avatar asked May 24 '12 19:05

Steeve Cannon


People also ask

How do I delete a session variable?

A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

How do I delete all session variables stored in the current session?

Which function is used to erase all session variables stored in the current session? Explanation: The function session_unset() frees all session variables that is currently registered.

Which function is used to delete session?

Session_destroy() function is used to destroy a session. This function destroys the complete session. To unset a single session variable, we can use the unset() function.

Can you edit session variables?

Session variables on the client are read-only. They cannot be modified.


2 Answers

[note: this answer is for Meteor 0.6.6.2 through at least 1.1.0.2]

[edit: updated to also explain how to do this while not breaking reactivity. Thanks to @DeanRadcliffe, @AdnanY, @TomWijsman, and @MikeGraf !]

The data is stored inside Session.keys, which is simply an object, so you can manually delete keys:

Session.set('foo', 'bar') delete Session.keys['foo']  console.log(Session.get('foo')) // will be `undefined` 

To delete all the keys, you can simply assign an empty object to Session.keys:

Session.set('foo', 'bar') Session.set('baz', 'ooka!') Session.keys = {}  console.log(Session.get('foo')) // will be `undefined` console.log(Session.get('baz')) // will be `undefined` 

That's the simplest way. If you want to make sure that any reactive dependencies are processed correctly, make sure you also do something like what @dean-radcliffe suggests in the first comment. Use Session.set() to set keys to undefined first, then manually delete them. Like this:

// Reset one value Session.set('foo', undefined) delete Session.keys.foo  // Clear all keys Object.keys(Session.keys).forEach(function(key){ Session.set(key, undefined); }) Session.keys = {} 

There will still be some remnants of the thing in Session.keyDeps.foo and Session.keyValueDeps.foo, but that shouldn't get in the way. 

like image 122
jpadvo Avatar answered Oct 16 '22 08:10

jpadvo


Session.set('name', undefined) or Session.set('name', null) should work.

like image 30
Tamara Wijsman Avatar answered Oct 16 '22 10:10

Tamara Wijsman