繁体   English   中英

google.script.run.withSuccessHandler(function) 不返回数据

[英]google.script.run.withSuccessHandler(function) not returning data

我正在使用 G-suite 并使用“google.script.run”功能。 我无法从从服务器获取数据到客户端的函数中获取或设置数据......我可以将数据设置为 #myId 的 DOM 并检查,但我想在后台进行......任何想法为什么这不起作用?

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;
  }
}

我需要能够使用常规数据设置 this.data ......或者至少从 this.getData() 返回它们

更新

我有

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

我的输出总是“给我测试”看起来辅助函数无法访问全局范围变量......

问题:

时差或google.script.run Async 性质:当时, submitData执行, this.datafalse ,因为getData()还没有完成执行。

                                                 ➚ 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

解决方案:

呼叫this.submitData()后, helper被调用。

片段:

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

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM