How can i pass a Map object with $session.$data?


#1

Hi all, in my code i have something like this:

let map = new Map();
...code to fill the map...
this.$session.$data.next1 = map; 

While testing i see that this.$session.$data :{"next1":{}} , so it’s empty.

Do i need to pass it as a JSON string using JSON.stringify() and doing what is explained here https://stackoverflow.com/questions/29085197/how-do-you-json-stringify-an-es6-map ??
Or is there some workarounds?


#2

I solved my problem without using Map and $session.$data. I needed to pass 2 param associated to each other after a button click. So, in the OptionItem i use .setKey(param1+’ '+param2) and then in ON_ELEMENT_SELECTED(), split the selectedElement.
Still, if there’s a way to pass a Map Object in $session.$data i would be happy to know.


#3

I am using a class with a map object. Its serialize method is called automatically after the handler middleware finished:

serialize() {
    return JSON.stringify([...this.flagMap]);
  } 

and for each request its constructor is used to create the object from the session store:

  constructor(dbObject?: string) {

    if(dbObject)
      this.flagMap = new Map(JSON.parse(dbObject));
  }

Does that help @Alberto1 ?