我有这个doPost网络应用程序,它从支付网关接收一个JSON对象。当存在内容时,它成功运行,但当内容为空或未定义时,我在GAS中的执行日志中得到一个失败的状态。
我尝试了几个不同的if语句,试图阻止失败的状态,但没有成功。下面是doPost函数,它包含一个if语句。
if(typeof e.postData === "undefined") return;如果e.postData等于“未定义”,任何人能提出跳过执行的解决方案吗?
function doPost(e) {
const jsonString = e.postData.getDataAsString();
const event = JSON.parse(jsonString)
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheetByName("Import");
const ts = new Date();
const time = Utilities.formatDate(ts, "GMT+1", "dd/MM/yyyy, h:mm a");
const nextRow = sh.getLastRow() + 1;
if(typeof e.postData === "undefined") return;
sh.getRange(nextRow, 1).setValue(time);
sh.getRange(nextRow, 2, 1, 7).setValues([[jsonString, event.data.risk.flagged, event.type, event.data.reference, event.data.amount, event.data.currency, event.data.customer.email]]);
}感谢您的阅读。
发布于 2022-10-01 15:02:03
在试用和错误之后,我发现了这个问题,通过使用问号使返回的数组中的每个元素都是可选的;下面是一个可选元素的示例:
event.data?.risk?.flagged最后的代码如下所示。有些元素没有包含在postData中,例如上面的元素。这可能是真、假或空。
function doPost(e) {
const jsonString = e.postData.getDataAsString();
const event = JSON.parse(jsonString)
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheetByName("Import");
const ts = new Date();
const time = Utilities.formatDate(ts, "GMT+1", "dd/MM/yyyy, h:mm a");
const nextRow = sh.getLastRow() + 1;
sh.getRange(nextRow, 1).setValue(time);
sh.getRange(nextRow, 2, 1, 7).setValues([[jsonString, event.data?.risk?.flagged, event.type, event.data?.reference, event.data?.amount, event.data?.currency, event.data?.customer.email]]);
getData()
}https://stackoverflow.com/questions/73901424
复制相似问题