Home > Net >  Cannot read property 'xyz' of undefined in Google Apps Script...even when variable is clea
Cannot read property 'xyz' of undefined in Google Apps Script...even when variable is clea

Time:04-07

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")

CodePudding user response:

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:

CodePudding user response:

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;
  • Related