Problem with the session.set

Hi,

I have one exec which contain the setter of the session (The id variable works, but the session.set does not work) :

`

.exec(session => {

maps.foreach(map => {
for ((k, v) ← map) {
if (k == “name” && v == “value”) {

val id: String = map(“id”).toString
session.set(“id_map”, id)

}
}
})

session
})
)

`

And another exec which use the id (but the id does not work because is not defined)

`
.exec(http(“My text”)
.get("/myUrit")
.queryParam(“id”, “${id_map}”)

}

`

Can you help me ?
I don’t understand why it reacts like that.

Thanks

Hi,

session is immutable, and you are using foreach which is a function that returns nothing. So the session that you return at the end of your exec is the same as the one you get at the beginning. Furthermore, that is not a correct way to seek an element in a Map.

And I don’t understand what you are doing : you are manually browsing a map, checking the value of the key and value, and if these are equals to what you want, you get another value with another key.

Why not just do

.exec { session =>
  val id = maps.flatMap { case (_, map) =>
    map.get("id")
  }.head.toString

  session.set("id_map", id)
}

Or remove the case and just :

  .exec { session =>
  val id = maps.flatMap { innerMap =>
    innerMap.get("id")
  }.head.toString

  session.set("id_map", id)

if maps is a List and not a Map

Thanks for you answer,

I check if the key is the name and if the value is “value” (“value” is an example ^^)
That means I want to check if the name is equal to “value”

Exactly, the maps is List of Map

All maps contain an id, and I want to set in session, only the id of the name “value”.

Sorry for my bad english.