'Cannot read property 'xyz' of undefined in Google Apps Script...even when variable is clearly defined
function doPost(e){
Logger.log(JSON.stringify(e));
const pr = JSON.stringify(e);
var a1 = pr.parameters.aoutlet;
AddRecord(a1);
}
// pr
//{"parameters":{"Bill-Amt":[""],"Vendor0":["ko"],"aoutlet":["GM"],"Vendor":["555"].....
And it Apps Script says... Cannot read property 'aoutlet' of undefined (line 13, file "Code")
Solution 1:[1]
In your situation, how about the following modification?
From:
function doPost(e){
Logger.log(JSON.stringify(e));
const pr = JSON.stringify(e);
var a1 = pr.parameters.aoutlet;
AddRecord(a1);
}
To:
function doPost(e){
Logger.log(JSON.stringify(e));
var a1 = e.parameters[0].aoutlet;
AddRecord(a1);
return ContentService.createTextOutput("ok");
}
or
function doPost(e){
Logger.log(JSON.stringify(e));
var a1 = e.parameter.aoutlet;
AddRecord(a1);
return ContentService.createTextOutput("ok");
}
Note:
- When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
- You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
Solution 2:[2]
Issue:
You're using JSON.stringify() on the e object, so pr is a string.
You are then trying to access properties in that string as if it was an object, and this is causing your error.
Solution:
Don't use JSON.stringify. Use e instead:
var a1 = e.parameters.aoutlet;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Iamblichus |
