0

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

LeSmoox
  • 37
  • 6
  • 1
    In Google Apps Script global variables are reevaluated on every execution, no matter if come from calling doGet or another function through `google.script.run`. If you need further help please add a [mcve]. – Rubén Oct 06 '22 at 20:15
  • Try keeping the variable in cacheService – Cooper Oct 06 '22 at 20:24
  • I would recommend you create a variable on the client side to store the uID. Then when ever you `google.script.run` pass the variable along with any other values. That way if you have multiple users accessing the web app there is no confusion about which user is making the server request. – TheWizEd Oct 06 '22 at 20:33
  • @Rubén you mean that each time i use `google.script.run` it reinitialize `fbuserIDkey` to "" (its first value) since it is global? If so, how should i store a value that i'll reuse elsewhere in several functions? I read that it was best practice to always declare empty global variable before updating it in functions. @Jared A Sutton it is declared above the doGet() function if that helps (see edit). Based on your comments I guess there is no obvious theorical answer to my problem but i might be ill written code right? – LeSmoox Oct 06 '22 at 20:41
  • Start by reading https://stackoverflow.com/q/24721226/1595451, Tl;Dr you might use the PropertiesService, the CacheService, the client side global scope, a Google Spreadsheet, among other options to store the persistent variables. – Rubén Oct 06 '22 at 21:28
  • Please include a minimal example that reproduces the issue. Please visit [How to Ask](https://stackoverflow.com/help/how-to-ask) have some tips on how to write a question, so the community will be able to help you out in a better way. – Lorena Gomez Oct 06 '22 at 21:38
  • @LorenaGomez we should keep the list of comments short. Considering this, please avoid to request something that was already requested ( in this case more code / mcve was requested two times) or at least explain why are you reiterating the same request. – Rubén Oct 06 '22 at 21:44
  • 1
    i totally get the point of the "mcve", but i have not much experience in developing so providing a "mcve" to reproduce the exact same thing will require me too much time i think (instead of trying again to solve it) and thinking of it is already giving me headaches. But reading through your comments, i've had half an answer already. I understand that the code behavior is "normal" since you are all advising to store the variable a different way (locally and passing it at the right moment, using cache service, spreadsheet, and so on).So thank you anyways i'll focus on rewriting the passing of it – LeSmoox Oct 06 '22 at 22:09
  • 1
    It's true writing a [mcve] can be time consuming. The good news for me is that I solve most of my problems on my own before I get it done, because it helps me to focus on the things that could be causing the problem. – Cooper Oct 06 '22 at 23:50

1 Answers1

1

Just answering the question since it is now solved if that can help anybody.

The trick as advised by the comments was indeed to get (again) the user.uID right at the moment I (re)need it, instead of trying to store it once and use it later. So, when document.getElementById("savepicks").addEventListener("click", StartSaving); happens, i get user info with onAuthStateChanged((user) => {if (user) {...}} and create one object with all the data I need from the client side. Then I pass it through google.script.run to the server side function and it works just fine!

Thanks to all who commented, I was just forcing it the wrong way.

Dhaval Gajjar
  • 2,508
  • 1
  • 2
  • 13
LeSmoox
  • 37
  • 6