我的代码正在数据库中搜索记录,并在找不到现有条目时返回错误。它需要在解析之前检查请求是否为空,如果未找到记录,则返回一个空数组,如果找到一个记录,则返回[results]数组。我该如何解决这个问题?
这是针对与Zoho CRM的Zapier集成,该集成将搜索自定义模块以Account_Name
查找现有记录,并创建一个尚不存在的记录。
const options = {
url: `https://www.zohoapis.com/crm/v2/Accounts/search?criteria=(Account_Name:equals:${bundle.inputData.Account_Name})`,
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Zoho-oauthtoken ${bundle.authData.access_token}`,
'Accept': 'application/json'
},
params: {
}
}
return z.request(options)
.then((response) => {
response.throwForStatus();
const results = [z.JSON.parse(response.content)];
return [results];
});
catch
如果您的诺言未能解决,您可以尝试使用。
喜欢:
return z.request(options)
.then((response) => {
response.throwForStatus();
const results = [z.JSON.parse(response.content)];
return [results];
})
.catch(err => {
/*
check if the error is specifically where no entry in db was found.
Or if the error is always for that purpose
*/
console.log(err) // handle error approriately, maybe send to sentry?
return []; //emtpy array, when db errors out?
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句