Zapier code - await is only valid in async function

365 Views Asked by At

I'm trying to pull data from Hubspot into Pendo using Zapier code (a recommendation from my Pendo rep). When testing using the code below I get "Syntax error: await is only valid in async function".

I have researched and tried making an async IIFE but that also didn't work. So, I'm wondering if there is an error somewhere else in my code causing the error, or, is there a better way to approach this rather than using await?

const data = [{
  "accountId": inputData.body.accountId,
  "values": {
     "Became Customer": inputData.body.becameCustomer,
     "Total MRR": inputData.body.totalMRR,
     "Company Owner": inputData.body.companyOwner
   }
}];
function updateAccount (z, bundle) {
  const promise = await fetch("https://app.pendo.io/api/v1/metadata/account/agent/value",     {
        method: "POST",
        body: JSON.stringify(data),
        headers: {
      "content-type": "application/json",
      "x-pendo-integration-key": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.XX"}
    });
  return promise.then((response) => {
    if (response.status != 200) {
      throw new Error(`Unexpected status code ${response.status}`);
    } else {
      const content = JSON.parse(response.content);
      return content;
    }
  });
}
updateAccount()```
1

There are 1 best solutions below

0
On

Wrapping the function in async got rid of the error.

const updateAccount = async function(z, bundle) {