无法读取 Google Apps 脚本中未定义的 属性 'xyz'...即使变量已明确定义

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"].....

它的 Apps 脚本说... 无法读取未定义的 属性 'aoutlet'(第 13 行,文件“代码”)

你的情况,下面的修改怎么样?

发件人:

function doPost(e){
  Logger.log(JSON.stringify(e));
  const pr = JSON.stringify(e);
  var a1 = pr.parameters.aoutlet;
  AddRecord(a1);
}

收件人:

function doPost(e){
  Logger.log(JSON.stringify(e));
  var a1 = e.parameters[0].aoutlet;
  AddRecord(a1);

  return ContentService.createTextOutput("ok");
}

function doPost(e){
  Logger.log(JSON.stringify(e));
  var a1 = e.parameter.aoutlet;
  AddRecord(a1);

  return ContentService.createTextOutput("ok");
}

注:

问题:

您在 e 对象上使用 JSON.stringify(),因此 pr 是一个字符串。

然后您试图访问该字符串中的属性,就好像它是一个对象一样,这导致了您的错误。

解决方案:

不要使用 JSON.stringify。使用 e 代替:

var a1 = e.parameters.aoutlet;