Issues with global variables in Google App Script

荒凉一梦 提交于 2020-08-10 22:59:46

问题


I'm trying to receive some data being sent to a webhook and work with it. I'm able to receive the data and convert it to a json String however I want to assign the json String to a global variable so I can use it elsewhere, and in other functions.

I'm declaring the variable first and then trying to assign the json string to it when i receive it but it doesn't seem to be working - the variable is still 'undefined'

var jsonData;

function doPost(e){
    try{
        var jsonString = e.postData.getDataAsString();
        setLog("***json String = " + jsonString + " ***");
        jsonData = JSON.parse(jsonString);
    }
    catch(e){           
        setLog("***Exception occured = "+JSON.stringify(e) + " ***");  
    }
}

I'm quite new to Javascript and I'm not sure where I'm going wrong with this.

When looking elsewhere I've found that global variables will be changed whenever the script runs as it's Google Apps Script, but I don't need these variables to remain the same after each time the script runs. I just need the global variable to use within other functions.


回答1:


You want to use jsonData at other functions. I think that there are 3 patterns for your situation.

Pattern 1

This is a simple solution. You redeclare jsonData in doPost(e). By this, undefined occurs. So modify as follows. In this case, jsonData is cleared after the running script was finished.

From :
var jsonString = e.postData.getDataAsString();
To :
jsonString = e.postData.getDataAsString(); // Remove "var"

Pattern 2

This is a solution that even if the running script is finished and it is run again, jsonData can be used. But there is a time limit. In this case, it uses Cache Service. When Cache Service is used, the value is required to be the string data.

function doPost(e){
    try{
        var jsonString = e.postData.getDataAsString();
        setLog("***json String = " + jsonString + " ***");
//        jsonData = JSON.parse(jsonString);
        var cache = CacheService.getScriptCache();
        cache.put('jsonString', jsonString, 1500); // cache for 25 minutes
    }
    catch(e){           
        setLog("***Exception occured = "+JSON.stringify(e) + " ***");  
    }
}

// When you use jsonData, please JSON.parse() like this function.
function myFunction() {
  var cache = CacheService.getScriptCache();
  var jsonString = cache.get('jsonString');
  var jsonData = JSON.parse(jsonString);

  // do something
}

Pattern 3

This is a solution that even if the running script is finished and it is run again, jsonData can be used. In this case, it uses Properties Service. When Properties Service is used, the value is required to be the string data and there is no time limit.

function doPost(e){
    try{
        var jsonString = e.postData.getDataAsString();
        setLog("***json String = " + jsonString + " ***");
//        jsonData = JSON.parse(jsonString);
        var scriptProperties = PropertiesService.getScriptProperties();
        scriptProperties.setProperty('jsonString', jsonString); // Save jsonString to the property.
    }
    catch(e){           
        setLog("***Exception occured = "+JSON.stringify(e) + " ***");  
    }
}

// When you use jsonData, please JSON.parse() like this function.
function myFunction() {
  var scriptProperties = PropertiesService.getScriptProperties();
  var jsonString = scriptProperties.getProperty('jsonString');
  var jsonData = JSON.parse(jsonString);

  // do something
}

If I misunderstand your situation, I'm sorry.



来源:https://stackoverflow.com/questions/49920041/issues-with-global-variables-in-google-app-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!