I have a web-app (GAS) where once the user has logged in (through Firebase/Google Auth), i am able catch his uID
. Since i need this uID
on server-sided functions later, once i catch it i run a function via google.script.run
that will update an already declared global variable so that to store it on the other side. This variable is declared outside any function as empty like this var fbuserIDkey = "";
when the app is loaded . So this variable, once the user has logged in, should have been updated with the uID
.
My problem is that later i need this variable in a function triggered by a click on a button, but the function server side seems to use an old version of the variable if that is possible, like it has not been updated.
Here are the bits of code involved:
declaring variable:
var fbuserIDkey = "";
passing the value when user is logged and updating server-side variable :
onAuthStateChanged{
...
const userIDkey = user.uid;
...
google.script.run.logUser(userIDkey);
} //console.log(userIDkey); = correct value
the function that updates global variable server-side:
function logUser(userIDkey){
fbuserIDkey = userIDkey;
Logger.log("ID is " + fbuserIDkey);//returns correct value on server-side too
}
So the variable has been updated.
But later, when the button is clicked on client-side and it triggers the function that should use the variable on server-side, it is still empty. Using Logger.log(fbuserIDkey)
returns ""
It looks like the variable has been updated but the function that actually needs it has the old version of it (""). How is that possible? I precise that the click on the button always happens far after everything is loaded. I guess there might be some trick due to server/client communication but i don't understand where exactly and how to correct it.
EDIT
So as suggested i add more context, trying to paste as much code as possible.
fbuserIDkey is declared on the file (.gs) that contains the doGet() function if it adds any relevant detail.
var fbuserIDkey = "";
function doGet(e) {
...}
below is the function triggered by button that uses the variable:
function TransferPicks(picks){
Logger.log("ID is" + fbuserIDkey);//returns ""
...}
the eventlister is:
document.getElementById("savepicks").addEventListener("click", StartSaving);
StartSaving
is a function that does several stuff (not related) and in the end triggers TransferPicks