Zapier You did not define output error when return and call included

685 Views Asked by At

When testing my Run Javascript action, I receive the following error

string: You did not define `output`! Try `output = {id: 1, hello: await Promise.resolve("world")};`

I don't understand why this is happening when my function includes a return and my code calls that function.

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

There are 1 best solutions below

0
On

Though your updateAccount() function correctly waits for the request to finish in itself, there's nothing to tell the Code by Zapier function to wait for updateAccount() to finish.

You also don't have to write a function at all here - the "Run Javascript" action in Code by Zapier already wraps your code in in an async function. Try the following:

const data = [
  {
    accountId: inputData.accountId,
    values: {
      "Became Customer": inputData.becameCustomer,
      "Total MRR": inputData.totalMRR,
      "Company Owner": inputData.companyOwner,
    },
  },
];

const response = await fetch(
  "https://app.pendo.io/api/v1/metadata/account/custom/value",
  {
    method: "POST",
    body: JSON.stringify(data),
    headers: {
      "content-type": "application/json",
      "x-pendo-integration-key": "<my integration key>",
    },
  }
);

if (response.status !== 200) {
  throw new Error(`Unexpected status code ${response.status}`);
} else {
  return response;
}