Hubspot-问题在MYSQL中添加与交易对应的公司名称

时间:2019-04-15 19:19:18

标签: javascript mysql fetch hubspot

当前,有一个脚本可以从Hubspot中获取有关交易的信息,并将其插入到MYSQL数据库中。这可以通过一个正常的提取调用来完成!

现在,当我尝试使用另一个提取调用添加与交易相关的公司名称时,我会遇到错误

(node:18772) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined

即使公司名称存在,在第二次访存调用之后,它仍然返回undefined。 这是我打的电话

var url='https://api.hubapi.com/deals/v1/deal/paged?hapikey=APIKEY&properties=dealname&properties=dealstage&properties=closedate&properties=dealtype&properties=type&properties=hubspot_owner_id&properties=amount&properties=notes_last_updated&includeAssociations=true';



function getHubspotData(url){
//Call to Get Deals
    fetch(url)
.then((resp) => resp.json()) // Transform the data into json
.then(function(data) {
  //console.log(JSON.stringify(data, null, 2));
  JSON.stringify(data, null, 2);

 offset=data.offset;
 hasMore=data.hasMore;


for(var i=0;i<data.deals.length;i++){


var dealId=data.deals[i].dealId;
var dealName=data.deals[i].properties.dealname.value;

var dealstage=data.deals[i].properties.dealstage.value;
if(dealstage=="721837bc-38c1-4891-80bc-0f7a96c55541"){
    dealstage="onhold";
}else if(dealstage=="dc937904-cc58-4b8c-b7a9-aa2f7f11609d"){
    dealstage="cooking";
}

if(data.deals[i].properties.hasOwnProperty('dealtype')==false){
var DealType="Deal Type Not Set";
}else{
var DealType=data.deals[i].properties.dealtype.value;
}
if(data.deals[i].properties.hasOwnProperty('type')==false){
var type="Type Not Set";
}else{
var type=data.deals[i].properties.type.value;
}

if(data.deals[i].properties.hasOwnProperty('notes_last_updated')==false){
    var LastActivityDate="0000-00-00";
}else{
var LastActivityDate_unix=data.deals[i].properties.notes_last_updated.value;
var LastActivityDate_unix=LastActivityDate_unix/1000;
var LastActivityDate=moment.unix(LastActivityDate_unix).format("YYYY-MM-DD");
}

var CloseDate_unix=data.deals[i].properties.closedate.value;
var CloseDate_unix=CloseDate_unix/1000;

var CloseDate=moment.unix(CloseDate_unix).format("YYYY-MM-DD");

var HubspotOwner=data.deals[i].properties.hubspot_owner_id.sourceId;
if(data.deals[i].properties.hasOwnProperty('amount')==false){
var Amount=0;
}else{
var Amount=data.deals[i].properties.amount.value;
}

if(dealstage=="a06c0bd0-b5b8-493d-876e-aa4169369247"){
dealstage="paperworkcomplete";
}


var CompanyID=data.deals[i].associations.associatedCompanyIds; 

if(CompanyID>0){

    //Calling the Company API

var url2="https://api.hubapi.com/companies/v2/companies/"+CompanyID+"?hapikey=APIKEY";

console.log("URL2: "+url2 );

fetch(url2)
.then((resp) => resp.json()) // Transform the data into json
.then(function(dataCompany) {

    JSON.stringify(dataCompany, null, 2);

    //#3: Company Name
    var CompanyName=dataCompany.properties.name.value;

var records= [ [dealId,CompanyName,dealName,dealstage,DealType,type,HubspotOwner,Amount,LastActivityDate,CloseDate] ];


connection.query('INSERT INTO Test (DealID,CompanyName,DealName,dealstage,DealType,Type,HubspotOwner,Amount,LastActivityDate,CloseDate) VALUES ?',[records]  , (err,rows) => {


console.log('Data received from Db: '+CompanyName);
//   console.log(rows);
}); 

}).catch((error) => {
    console.log("Error Getting the Company Name: "+error+" CompanyID: "+CompanyID);
  });

} else{
    CompanyID=0;
    CompanyName="Company Name not Set";
    var records= [ [dealId,CompanyName,dealName,dealstage,DealType,type,HubspotOwner,Amount,LastActivityDate,CloseDate] ];
    connection.query('INSERT INTO Test (DealID,CompanyName,DealName,dealstage,DealType,Type,HubspotOwner,Amount,LastActivityDate,CloseDate) VALUES ?',[records]  , (err,rows) => {


        console.log('Data received from Db:'+CompanyName);
        //   console.log(rows);
        });
}



}
console.log("Outside the For Loop");
console.log("Length: "+data.deals.length);
if(hasMore==true){
var url='https://api.hubapi.com/deals/v1/deal/paged?hapikey=4e1866fc-f495-4fae-99e4-a7231c0f32b1&properties=dealname&properties=dealstage&properties=closedate&properties=dealtype&properties=type&properties=hubspot_owner_id&properties=amount&properties=notes_last_updated&includeAssociations=true&offset='+offset;
if(url){getHubspotData(url);}
else{
    console.log("URL is undefined.");
}
 }
 else{
     process.exit();
 }

  } ).catch((error) => {
    console.log("Error Getting Deals: "+error);
  }); }

在此问题上的任何帮助将不胜感激!

0 个答案:

没有答案