What I’m trying to do:
I am trying to access anvil.server.session data, but it is empty.
What I’ve tried and what’s not working:
I set some session values in function A. I then call function B from within function A and the session variables are not set.
Code Sample:
# this is a very cut down snippet of code.
@anvil.server.callable
def authenticate_user(**kwargs):
username = kwargs.get("username", None)
password = kwargs.get("password", None)
res = anvil.server.call("uplink_login", username=username, password=password)
if res.status == 200:
anvil.server.session['username'] = username
anvil.server.session['email'] = res.data[0]['email']
anvil.server.session['role_name'] = res.data[0]['role_name']
debug.print(anvil.server.session) # <---THEY ARE SET HERE
return Response(200, [anvil.server.call("fetch_safe_data")], "")
else:
return Response(401, [], "login failed")
@anvil.server.callable
def fetch_safe_data():
debug.print(anvil.server.session) # <--- THEY ARE NOT SET HERE
ret = {
"username": anvil.server.session['username'],
"role_name": anvil.server.session['role_name'],
"email": anvil.server.session['email']
}
return ret
Is this expected behaviour? If so, why? Or am I doing something dazzlingly stupid?
EDIT - If I “un-decorate” fetch_safe_data() so it is a “normal” function, the problem goes away. So it’s something to do with the decorated function not having the session data yet. So again - is this expected behaviour?