我正在尝试从Hubspot获取所有交易,但由于某些原因,我在某些交易中遇到了错误。
错误的类型为Cannot read property '89' of undefined
。
var fetch = require("node-fetch");
getHubspotData('https://api.hubapi.com/deals/v1/deal/paged?hapikey=demo&properties=dealname&properties=dealstage&properties=closedate&properties=dealtype&properties=type&properties=hubspot_owner_id&properties=amount&properties=notes_last_updated&includeAssociations=true');
async function getHubspotData(url) {
console.log("URL: " + url);
var iterations = 0;
for (let i = 0; i < 100; i++) {
await fetch(url)
.then((resp) => resp.json()) // Transform the data into json
.catch((error) => {
console.log("Error processing JSON: " + error)
}).then(function (data) {
console.log("Then-3");
iterations = iterations + 1;
console.log("I: " + iterations);
if ((data.hasMore == false) && (i == data.deals.length)) {
console.log("Synced all Deals From Hubspot");
console.log("Program Terminating...........................................");
process.exit();
}
if (data === undefined) {
console.log("data is undefined//////////////////////////////////////////////////////////////////////");
fetch(url)
.then((resp) => resp.json())
.then(function (data) {
console.log(data.deals.length);
}
)
} else {
console.log("data is NOT undefined");
}
var dealId = data.deals[i].dealId;
var dealName = data.deals[i].properties.dealname.value;
console.log(dealName);
offset = data.offset;
hasMore = data.hasMore;
dealLength = data.deals.length;
if ((hasMore == true) && (i + 1 >= dealLength)) {
console.log("NEW LOOP>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
var url = 'https://api.hubapi.com/deals/v1/deal/paged?hapikey=demo&properties=dealname&properties=dealstage&properties=closedate&properties=dealtype&properties=type&properties=hubspot_owner_id&properties=amount&properties=notes_last_updated&includeAssociations=true&offset=' + offset;
getHubspotData(url);
}
}).catch((error) => {
console.log("Error getting the deals //////////////////////////////////////////////////////////// " + error)
})
}
它似乎是随机的。在一次执行中,它可能会为交易69-Cannot read property '69' of undefined
引发错误,而下一次我运行它时,它将在其他一些交易中引发错误。
我尝试使用if条件检查数据是否为undefined
,但这无济于事。不知道为什么我收到此错误,找不到解决此问题的方法。
在此问题上的任何帮助将不胜感激!