0

I'm checking with HubSpot's official documents, MDN pages and past question on Stack overflow, but I've been stuck for the past few weeks. I'd appreciate any thoughts or ideas.

My goal is to request API from the HTML file that contains JavaScript. (As I'm testing and troubleshoooting the codes of a Workflow's custom actions on VSC so afar, I don't need to use any other tools like Postman.)

▶︎ Test codes (1)

`<script>

    const temp = 12345;

    const hubspot = require('@hubspot/api-client');
    const hubspotClient = new hubspot.Client({"accessToken":"***************"});
    const URL = `https://api.hubapi.com/crm/v3/objects/contacts/search`;

    // https://www.npmjs.com/package/@hubspot/api-client
    const filter = { propertyName: 'temp', operator: 'EQ', value: `${temp}` }
    const filterGroup = { filters: [filter] }
    const limit = 100
    const after = 0 // after for initial search should be set as 0

    publicObjectSearchRequest = async () => 
    {
        filterGroups: [filterGroup]
        limit,
        after
    }

    const result = await hubspotClient.crm.contacts.searchApi.doSearch(publicObjectSearchRequest)
    console.log(JSON.stringify(result))`

</script>

▷ This code is based on this cocument Since the sample codes in the above page didn't include ▷ Result (1) 【console error】Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modulesResult at the line of "const result = await hubspotClient.crm.contacts.searchApi.doSearch(publicObjectSearchRequest)"

▶︎ Test codes (2)

<script>

    const temp = 12345;

    const hubspot = require('@hubspot/api-client');
    const hubspotClient = new hubspot.Client({"accessToken":"***************"});
    const URL = `https://api.hubapi.com/crm/v3/objects/contacts/search`;



    searchID = async () => 
    {
      const settings = 
        {
            method: 'POST',
            headers: 
                {
                  'Accept': 'application/json',
                  'Content-Type': 'application/json; charset=UTF-8'
                },
            body: JSON.stringify
                ({"filterGroups":
                    [
                        {"filters":
                            [
                                {
                                "propertyName":"temp",
                                "operator":"EQ",
                                "value":temp
                                }
                            ]
                        }
                    ]
                })

        };
      try {
            const fetchResponse = await fetch(URL);
            const data = await fetchResponse.json();  //with "await," don't need ".then((res) => res)"
            return data;
          } 
        catch (err) {
                    return err;
                  }    

}

    
    const searchRes = responce.json();
    console.log(`search API responce : ${searchRes}`);
    console.log(`total : ${searchRes.total}`);

▷ Result (2)【console error】Uncaught ReferenceError: require is not defined at the line of "const hubspot = require('@hubspot/api-client');" Following the offical document, I downloaded the packeages using "npm install -g @hubspot/api-client" - but still this occurs

Alice
  • 21
  • 1
  • 6
  • `require` doesn't exist in browsers. You'd either use a 3rd party library which adds it, or reference your additional scripts from in-browser code some other way. (Such as adding it in another ` – David Mar 20 '23 at 19:15
  • Hi @David, thank you for taking a look into this. Oh then, this is strange that I didn't get "Uncaught ReferenceError:" for the 1st codes... Node.js and hubspot/api-client are installed on my laptop, so I thought it'd work for my local files. (This html file is saved locally and not hosted in the domain. So I'm not worried about the access token.) – Alice Mar 21 '23 at 01:26
  • Regardless of what's installed on the computer, the ` – David Mar 21 '23 at 11:42
  • I don't have access to server and that's why I tested things on VSC and the browser. I guess I just need to implement the test codes from the HubSpot application then – Alice Mar 21 '23 at 17:19

0 Answers0