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...

9
  • 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(); Commented Oct 29, 2020 at 10:31
  • 1
    You need to wait until data is retrieved. Commented Oct 29, 2020 at 10:36
  • @Reyno - nope did not help... Commented Oct 29, 2020 at 10:39
  • My output is always "give me test" How are you getting the output? Commented Oct 29, 2020 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 Commented Oct 29, 2020 at 12:49

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

2 Comments

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 Could you edit your question to provide a minimal reproducible example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.