I have a little problem with using client-server communication in Google Apps Script Web App.
My code.gs contains:
function loadPetName() {
Logger.log('Client ask a Pet Name');
// use Google apps UserProperties as default storage
var storage = PropertiesService.getUserProperties();
return JSON.parse(storage.getProperty('name');
}
Pet.html is:
<script>
// CLASS Pet
function Pet() {
// CONSTRUCTOR
console.log('Creating new Pet');
var petName;
// Get pet name from outer storage
console.log('Loading pet name from storage...');
google.script.run.withSuccessHandler(onSuccessLoad).loadPetName();
var onSuccessLoad = function(name) {
console.log('run onSuccessLoad');
this.petName = name;
alert('Pet Name is ' + name)
};
// METHODS ...
}
</script>
init.html is:
<script>
function init() {
console.log('Initialization');
var cat = new Pet();
}
</script>
index.html contains:
<body onload=init()>
...
After index.html opened in browser I see in console:
Initialization
Creating new Pet
Loading Pet Name from storage...
But there is no "run onSuccessLoad".
In Google Script console we can see: "Client ask a Pet Name". All works fine, but onSuccessLoad function isn't run.
If in Pet.html we replace
google.script.run.withSuccessHandler(onSuccessLoad).loadPetName();
to
google.script.run.withSuccessHandler(console.log('SuccessHandler fired')).loadPetName();
We will see in console: "SuccessHandler fired".
Where is my problem?
Thanks!
I found a mistake... OMG!
This code doesn't work:
google.script.run.withSuccessHandler(onSuccessLoad).loadPetName();
var onSuccessLoad = function(name) {
console.log('run onSuccessLoad');
this.petName = name;
};
But this code works fine:
var onSuccessLoad = function(name) {
console.log('run onSuccessLoad');
this.petName = name;
};
google.script.run.withSuccessHandler(onSuccessLoad).loadPetName();
But WHY?! (Sorry for the stupid question).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.