如何在自定义Zapier应用中获取Xero API返回的指定嵌套字段值
实现代码(可直接替换你现有的API端点配置逻辑)
const options = { url: `https://api.xero.com/api.xro/2.0/Contacts/${bundle.inputData.ContactID}`, method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': `Bearer ${bundle.authData.access_token}`, 'xero-tenant-id': process.env.TENANT_ID }, } return z.request(options) .then((response) => { response.throwForStatus(); const results = response.json; // 以下为提取FAX号码的逻辑 const targetContact = results.Contacts?.[0] || {}; const faxEntry = targetContact.Phones?.find(item => item.PhoneType === 'FAX') || {}; const faxNumber = faxEntry.PhoneNumber || ''; // 将提取好的FAX号码存入返回结果,后续在Zapier字段列表中可直接选取使用 results.faxNumber = faxNumber; return results; });
逻辑说明
- 你调用的是指定ContactID的单个联系人查询接口,返回结果的
Contacts数组永远只有1条对应联系人数据,所以直接取数组第一个元素即可 - 使用
Array.find()方法遍历Phones数组,匹配PhoneType为FAX的条目 - 增加了空值兼容逻辑:如果没有找到对应FAX条目,会返回空字符串,不会触发代码报错
- 最终提取的FAX号码会作为
faxNumber字段和原有接口返回字段一起返回,你可以直接在Zapier的后续步骤中选取该字段使用
内容的提问来源于stack exchange,提问作者Tom Mitchell
相关产品推荐
相关产品推荐

