[英]google.script.run.withSuccessHandler(function) not returning data
I'm working with G-suite and using "google.script.run" function.我正在使用 G-suite 并使用“google.script.run”功能。 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?我无法从从服务器获取数据到客户端的函数中获取或设置数据......我可以将数据设置为 #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;
}
}
I need to be able to set this.data with with regular data...or atleast return them from this.getData()我需要能够使用常规数据设置 this.data ......或者至少从 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...我的输出总是“给我测试”看起来辅助函数无法访问全局范围变量......
Time difference or Async nature of google.script.run
: At the time, submitData
executes, this.data
is false
, because getData()
hasn't finished executing.时差或google.script.run
Async 性质:当时, submitData
执行, this.data
为false
,因为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
Call this.submitData()
after helper
is called.呼叫this.submitData()
后, helper
被调用。
function helper(data){
$('#myId').html('data'); // => works...
this.data = data;
this.submitData();// => works
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.