首页 > 解决方案 > GatsbyJS : 将调用 Hubspot API 的脚本放在哪里

问题描述

我需要将用户信息从 Hubspot 提取到我在 AWS Amplify 上托管的 Gatsby 站点。在他们的文档中,在 NodeJS 下,他们提供了一些示例代码:

var request = require("request");

var options = { method: 'GET',
  url: 'https://api.hubapi.com/contacts/v1/contact/vid/12627374/profile',
  qs: { hapikey: 'demo' }
}
request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

我只是不确定该代码应该在哪里,以便我可以动态提取数据。它应该放在 gatsby-browser.js 下吗?还是我需要一个额外的模块,比如 Axios 或 Express ?还是它在我的 AWS 安装中的某个地方?

谢谢 !

标签: gatsbyhubspot

解决方案


在 return 语句之前,我在组件中使用它获得了交易数据:

export default function AppHub () {

    const [deals, setDeals] = useState([]);
    
    useEffect(() => {
    
        fetch(`https://api.hubapi.com/crm/v3/objects/deals?hapikey=YOURKEYHERE&limit=100`,
        )
        .then(response => response.json()) // parse JSON from request
        .then(dealsapi => {
            console.log(dealsapi);
            setDeals(dealsapi.results);
        })
        .catch(console.error);
    }, []);

推荐阅读