How can I retrieve all `Session` values without knowing the `key`?

I want to add a new function to my app to retrieve all Session key/values at a specific point in time (basically when a specific error happens) and then send this either to the Backend server or to a log file.

I know that this was something that MeteorToys originally could do as it listed them.

How can this be achieved without know the key of each Session?

I know it’s a reactive dictionary underneath from the 'meteor/session' package.

Thanks in advance!

Try Session.all().
// I don’t know if it works :slight_smile:

It does work in the Developer Tools but now I need to figure out how I can do it in the code/Electron app itself and then store the result in array of objects.

Any idea, Minh?

I have no experience working with Electron app. I can’t tell, but if Session works then it should work.

Edit: works with Session.keys and returns the object of keys-values.


Try Session.keyValueDeps . The result should be the object with all your session keys.

For each key, get the values with Session.get()

Session.keyValueDeps gives me this result:

Screenshot 2023-03-01 at 01.21.32

I don’t know which key is in the Session object, nor do I know how many there are.

Could you show me a small code example how I can iterate over it and read out all key/values?

Session.keys will give you and object with all keys/values.

const obj = Session.keys;
            console.log(`Session: ${JSON.stringify(obj)}`);

gives me an empty object.

Try

Object.keys(Session.keys).forEach((key) => {console.log(key)})

Thanks for the code, however it’s still not showing any content. The object remains empty

That is ok. It means you have no key :). Just initiate a key and it should probably work. You can run it in a browser console. I believe you work on Electron, just try it in a browser to make sure Electron is not bugging you on this one.

Screen Shot 2023-03-01 at 12.48.36 PM

Hi,

I do have lots of keys (over 50), hence my question. The old version of Meteortoys even allowed them to be overlayed in Dev environment.

Yes, I work in Electron but it has the Chrome Developer Tool from which is the screenshot.

So I’m puzzled why the code isn’t showing anything, no matter where I put it when there are indeed many keys that I actively use to show/hide UI elements, indicate if features are unlocked etc.

You might have keys in the code, but unless a client is actually initialing them they will not show.

The app does initiate the keys (assuming you mean .get and .set).

My solution will need a lot of refactoring but might solve your problem. I’m creating enums for nearly everything including pub/subs, collection names, event names, session keys etc… Just walking in this enum object will give what you want with typo safety and ide suggestions.

export const SESSION_KEY = {
  ORDER_ID: 'orderId',
  PRODUCT_ID: 'productId',
  PRODUCT_VARIATION: 'variation',
  PRODUCT_ADDITION: 'addition',
  PRODUCT_ADDITION_GROUP: 'additionGroupId',
  CUSTOMER_ID: 'customerId',
  TABLE_ID: 'tableId'
};

Besides that, using session means using globals and you should avoid it as much as possible. Instead you can use reactive vars which will be destroyed on template desctruction.

1 Like

Thanks for coming up with an alternative solution, interesting approach.

Please note that I need those “flags” on a global level and not just on a template.

global level vars can be declared without var or const

SESSION_KEY = {
  ORDER_ID: 'orderId',
  PRODUCT_ID: 'productId',
  PRODUCT_VARIATION: 'variation',
  PRODUCT_ADDITION: 'addition',
  PRODUCT_ADDITION_GROUP: 'additionGroupId',
  CUSTOMER_ID: 'customerId',
  TABLE_ID: 'tableId'
}```
1 Like

Session.all() gives you an object you can iterate trough properties;

for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
        // do stuff
    }
}