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