You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js集成HubSpot与Pipedrive同步:数据无法更新问题排查

问题分析与修复方案

你遇到的核心问题有两个:

  1. 条件判断语法错误:代码中条件判断里的逻辑与运算符&&被错误写成了HTML转义字符&&,导致条件判断失效——即使联系人数据完整,也会误判为“数据不完整”进入错误分支。
  2. HubSpot API数据结构处理错误:HubSpot CRM v3 API返回的联系人属性是直接字符串值,并非嵌套的value对象,但你的代码错误地用.value访问属性值,会导致取值失败。

另外还有一个日志输出小问题:同步函数映射的pipedriveContact没有firstname和lastname字段,但后续更新/创建函数里的日志却尝试打印这两个字段,会输出undefined。


修复后的完整代码

const axios = require('axios');
require('dotenv').config(); // Load environment variables

// Pipedrive API credentials
const pipedriveApiKey = process.env.PIPEDRIVE_API_TOKEN; // Use dotenv for secret management
const pipedriveApiUrl = 'https://Rian-Sandbox.pipedrive.com/v1';

// HubSpot API credentials
const hubspotApiKey = process.env.HUBSPOT_API_KEY; // Use dotenv for secret management
const hubspotApiUrl = 'https://api.hubapi.com/crm/v3/objects/contacts';

// Axios configurations for headers
const pipedriveConfig = {
  headers: {
    'Authorization': `Bearer ${pipedriveApiKey}`,
  },
};

const hubspotConfig = {
  headers: {
    'Authorization': `Bearer ${hubspotApiKey}`,
  },
};

// Function to get contacts from HubSpot
async function getHubSpotContacts() {
  try {
    const response = await axios.get(`${hubspotApiUrl}`, {
      params: {
        limit: 100, // Adjust the limit as needed
      },
      headers: hubspotConfig.headers,
    });

    const hubSpotContacts = response.data.results; // Adjust the property based on the actual response structure

    if (!Array.isArray(hubSpotContacts)) {
      console.error('HubSpot contacts response is not an array:', hubSpotContacts);
      return; // Exit the function or handle the error as needed
    }

    return hubSpotContacts;
  } catch (error) {
    console.error('Error fetching HubSpot contacts:', error.message);
    throw error;
  }
}

// Function to create or update a contact in Pipedrive
async function createOrUpdatePipedriveContact(contact) {
  try {
    // Check if the contact already exists in Pipedrive based on email
    const response = await axios.get(`${pipedriveApiUrl}/persons/find`, {
      params: {
        term: contact.email,
      },
      headers: pipedriveConfig.headers,
    });

    const existingPerson = response.data.data[0];

    if (existingPerson) {
      // Update the existing person
      const updatedResponse = await axios.put(`${pipedriveApiUrl}/persons/${existingPerson.id}`, contact, {
        headers: pipedriveConfig.headers,
      });
      console.log('Contact updated in Pipedrive:', contact.name);
      return updatedResponse.data.data;
    } else {
      // Create a new person in Pipedrive
      const createdResponse = await axios.post(`${pipedriveApiUrl}/persons`, contact, {
        headers: pipedriveConfig.headers,
      });
      console.log('Contact created in Pipedrive:', contact.name);
      return createdResponse.data.data;
    }
  } catch (error) {
    console.error('Error creating/updating Pipedrive contact:', error.message);
    throw error;
  }
}

// Function to sync contacts from HubSpot to Pipedrive
async function syncHubSpotToPipedrive() {
  try {
    const hubSpotContacts = await getHubSpotContacts();

    if (!Array.isArray(hubSpotContacts)) {
      console.error('HubSpot contacts response is not an array:', hubSpotContacts);
      return; // Exit the function or handle the error as needed
    }

    for (const contact of hubSpotContacts) {
      if (
        contact.properties &&
        contact.properties.firstname &&
        contact.properties.lastname &&
        contact.properties.email
      ) {
        // Customize the contact mapping according to your needs
        const pipedriveContact = {
          name: `${contact.properties.firstname} ${contact.properties.lastname}`,
          email: contact.properties.email,
          phone: contact.properties.phone || '',
        };

        await createOrUpdatePipedriveContact(pipedriveContact);
      } else {
        console.error('Contact data is incomplete or missing:', contact);
      }
    }

    console.log('HubSpot to Pipedrive sync completed.');
  } catch (error) {
    console.error('Error during HubSpot to Pipedrive sync:', error.message);
  }
}

// Function to start the synchronization
async function startSync() {
  await syncHubSpotToPipedrive();
}

// Call the function to start the synchronization
startSync();

关键修复点说明

  1. 修复条件判断运算符:将同步函数if条件里的&&替换为正确的JavaScript逻辑与运算符&&,确保条件判断能正常识别完整的联系人数据。
  2. 修正HubSpot属性访问方式:去掉.value后缀,直接使用contact.properties.firstname、contact.properties.email等获取属性值,匹配HubSpot v3 API的返回结构。
  3. 修复日志输出错误:将创建/更新函数中的日志打印字段从contact.firstname, contact.lastname改为contact.name,因为映射后的pipedriveContact只包含name字段。
  4. 简化phone字段处理:用contact.properties.phone || ''替代三元表达式,逻辑更简洁。

内容的提问来源于stack exchange,提问作者Student

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 00:16:09