0

I'm working with G-suite and using "google.script.run" function. I can't fetch or set data out of the function that get's me data to client from server...I can set data to DOM for #myId and check that but I'd like to do it on background...any idea why is this not working?

function Class(){
  this.data = false;

  this.getData = function(){
    google.script.run.withSuccessHandler(helper).getData();
      function helper(data){
        $('#myId').html('data'); // => works...
        this.data = data; // => does not work...
        return data; // => does not work...
      }
  }

  this.submitData = function(){
    if(this.data === false){
      alert('no data');
    }
    else{
      ...code...
    }
  }

  this.run = function(){
    this.getData();
    this.submitData(); // => always get false;
  }
}

I need to be able to set this.data with with regular data...or atleast return them from this.getData()

UPDATE

I've got

this.getData = function(){
  var testOuput = 'give me test';
    google.script.run.withSuccessHandler(helper).getData();
      function helper(data){
        testOuput = 'give me something else';
      }
  return testOutput;   
}

this.run {
  alert(this.getData());
}

this.run() // => runs on button click

My output is always "give me test" It looks like helper function is not able to access GLOBAL SCOPE variables...

Orbit
  • 31
  • 4
  • What is the output if you `console.log(this)` inside the helper function? It could point to a different object so try the following: `google.script.run.withSuccessHandler(helper.bind(this)).getData();` – Reyno Oct 29 '20 at 10:31
  • 1
    You need to wait until data is retrieved. – TheMaster Oct 29 '20 at 10:36
  • @Reyno - nope did not help... – Orbit Oct 29 '20 at 10:39
  • *My output is always "give me test"* How are you getting the output? – TheMaster Oct 29 '20 at 11:31
  • Maybe youR Successhandler never gets called because the Apps Script function getData() fails? Implete a FailureHandler to verify. Also, provide the code of getData() for troubleshooting if you think this might be the cause of the issue – ziganotschka Oct 29 '20 at 12:49
  • Issue is the same as said in my answer. At the time ``helper`` function is called, `getData` has finished running. Read my answer until you get what it means. Read all the answers in the linked references until you're clear with "callback"s. – TheMaster Oct 29 '20 at 13:12
  • @ziganotschka If the issue was "server" function failure, `$('#myId').html('data');` shouldn't have worked. – TheMaster Oct 29 '20 at 13:14
  • @theMaster True, I skipped this bit. – ziganotschka Oct 29 '20 at 13:20
  • where's your doGet? – Baby_Boy Oct 30 '20 at 17:30

1 Answers1

1

Issue:

Time difference or Async nature of google.script.run: At the time, submitData executes, this.data is false, because getData() hasn't finished executing.

                                                 ➚ Server executes `getData()` => Calls `helper(datafromServer)` after execution 
                                               ➚  
`this.run` => `this.getData()` => Server called with `google.script.run` and  returns void immediately 
                                                (Client will not wait for server/async) 
                                               ➘ `this.submitData()` called => `helper()` hasn't finished executing. `this.data` is still false

Solution:

Call this.submitData() after helper is called.

Snippet:

  function helper(data){
    $('#myId').html('data'); // => works...
    this.data = data; 
    this.submitData();// => works
  }

References:

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • I understand what you mean but this is not the case...maybe it's my bad that I put to function run both of the functions but in reality I run function getData before at least a minute to read them in fact I can see them on screen been saved in DOM...user manipulate on client site with this data and after maybe a minute I run submit function... – Orbit Oct 29 '20 at 11:12
  • @Orbit Could you edit your question to provide a [mcve]? – TheMaster Oct 29 '20 at 11:28