Javascript 访问不存在属性的安全方法
我正在使用GoogleApps脚本和JavaScript代码,运行UrlFetchApp,从条带API中提取数据以检索发票。然后,该脚本将API中的数据更新为GoogleSheet模板 我遇到的问题是,如果API没有针对某个客户的特定行的数据,它就会破坏脚本。API将缺少的数据存储为NULL,例如当客户在发票上没有折扣时,“折扣”:NULL 当脚本到达没有数据的行时,NULL响应将中断脚本并停止运行。我想做的是返回一个值,指定没有数据(例如:返回数字0表示没有折扣),并保持脚本在其余代码行中运行 我的代码:Javascript 访问不存在属性的安全方法,javascript,json,Javascript,Json,我正在使用GoogleApps脚本和JavaScript代码,运行UrlFetchApp,从条带API中提取数据以检索发票。然后,该脚本将API中的数据更新为GoogleSheet模板 我遇到的问题是,如果API没有针对某个客户的特定行的数据,它就会破坏脚本。API将缺少的数据存储为NULL,例如当客户在发票上没有折扣时,“折扣”:NULL 当脚本到达没有数据的行时,NULL响应将中断脚本并停止运行。我想做的是返回一个值,指定没有数据(例如:返回数字0表示没有折扣),并保持脚本在其余代码行中运行
function getInvoiceObj()
{
var apiKey, content, options, response, secret, url;
secret = "rk_live_xxxxxxxxxxxxxxxxxx";
apiKey = "xxxxxxxxxxxxxxxxx";
url = "https://api.stripe.com/v1/invoices/in_xxxxxxxxxxxxx?expand[]=charge&expand[]=customer";
options = {
"method" : "GET",
"headers": {
"Authorization": "Bearer " + secret
},
"muteHttpExceptions":true
};
response = UrlFetchApp.fetch(url, options);
//Push data to Sheet from invoice. **Writes over existing Sheet data**
content = JSON.parse(response.getContentText());
var sheet = SpreadsheetApp.getActiveSheet();
/* Customer Discount */
sheet.getRange(21,2).setValue([content.discount.coupon.percent_off]);
}
你可能在找
或许
你可能在找
或许
这是一个有点棘手的问题,从现在起多年都会受到影响,您的问题是
折扣中不存在优惠券
,因此无法从未定义的属性中获取任何值(因此脚本中断),有几种技术可以从不存在的对象访问属性,例如:
try…catch
结构
try {
sheet.getRange(21,2).setValue([content.discount.coupon.percent_off]);
} catch() {
sheet.getRange(21,2).setValue([0]);
}
- 可选对象传递
const discountValue = ((content.discount || {}).coupon || {}).percent_off || 0;
sheet.getRange(21,2).setValue([discountValue]);
- 嵌套存在
const discountValue = (content.discount && content.discount.coupon && content.discount.coupon.percent_off) || 0;
sheet.getRange(21,2).setValue([discountValue]);
- 关于财产存取的若干抽象
const getPropertySafe = (props, object) => props.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, object)
const value = getPropertySafe(['discount', 'coupon', 'percent_off'], content)
sheet.getRange(21,2).setValue([value || 0]);
不过,还是要等待存在运算符
content.discount?.coupon?.percent|off | | 0
这是一个有点棘手的问题,从现在起多年都会发生,您的问题是coupon
中不存在折扣
,因此无法从未定义的属性中获取任何值(因此脚本中断),有几种技术可以从不存在的对象访问属性,例如:
结构try…catch
try { sheet.getRange(21,2).setValue([content.discount.coupon.percent_off]); } catch() { sheet.getRange(21,2).setValue([0]); }
- 可选对象传递
const discountValue = ((content.discount || {}).coupon || {}).percent_off || 0; sheet.getRange(21,2).setValue([discountValue]);
- 嵌套存在
const discountValue = (content.discount && content.discount.coupon && content.discount.coupon.percent_off) || 0; sheet.getRange(21,2).setValue([discountValue]);
- 关于财产存取的若干抽象
const getPropertySafe = (props, object) => props.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, object) const value = getPropertySafe(['discount', 'coupon', 'percent_off'], content) sheet.getRange(21,2).setValue([value || 0]);
内容。折扣?优惠券?百分之| | 0
不,你不想这么做。由于某种原因,0与null不同。相反,请正确处理空值。不,您不想这样做。由于某种原因,0与null不同。相反,请正确处理空值。谢谢!我使用了第二个选项,条件运算符,它成功了。我感谢你的帮助!谢谢我使用了第二个选项,条件运算符,它成功了。我感谢你的帮助!