Fetch error in pack building

Hi community,

I am new to pack building, don’t have experience in JavaScript as well. I am having trouble building the following code which connect Coda and Majestic.com. The errors are

  • On line 22 Cannot find name ‘fetch’.
  • On line 31 Cannot find name ‘fetch’.
    I am sure there is something I am not understanding about the SDKs in Coda. Please help!
    Note: Keys/IDs data has been kept blank intentionally here in the post.

import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();

// Set up authentication credentials for Coda API
const codaAPIKey = '';
const codaDocId = '';
const codaTableId = '';

// Set up authentication credentials for Majestic API
const majesticAPIKey = '';

// Define the list of websites to retrieve trust flow data for
const websites = [
  { name: 'coda', rowId: 'your-coda-row-id' },
  { name: 'google', rowId: 'your-coda-row-id' },
  { name: 'facebook', rowId: 'your-coda-row-id' },
  // Add more websites as needed
];

// Define a function to retrieve the trust flow data for a given website from Majestic API
async function getTrustFlowData(website) {
  const response = await fetch(`https://api.majestic.com/api/json?app_api_key=${majesticAPIKey}&cmd=GetBacklinkData&item=${website.name}&Count=1`);
  const data = await response.json();
  const trustFlow = data.DataTables.Results.Data[0].TrustFlow;
  return trustFlow;
}

// Define a function to update the Coda table with the retrieved trust flow data
async function updateCodaTable(website) {
  const trustFlow = await getTrustFlowData(website);
  const response = await fetch(`https://coda.io/apis/v1/docs/${codaDocId}/tables/${codaTableId}/rows/${website.rowId}/cells`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${codaAPIKey}`
    },
    body: JSON.stringify({
      'cells': [
        {
          'column': 'Website',
          'value': website.name
        },
        {
          'column': 'Trust Flow',
          'value': trustFlow
        }
      ]
    })
  });
  const data = await response.json();
  console.log(data);
}

// Call the updateCodaTable function for each website in the list
websites.forEach(website => updateCodaTable(website));

Hi @The_Lab - Welcome to the Coda community! We aim to enable non-professional coders to build a Pack, but it will be a bumpy ride if you don’t have any experience with JavaScript.

Thanks for sharing your code so far, here are some of the main issues with it:

  • It’s not using the Pack SDK to create any building blocks. Unlike scripts that can be run directly, Packs must define building blocks like formulas, actions, etc.
  • The fetch() call you are using it not available in the Packs runtime, instead you must use the proprietary Fetcher interface.

It may be a good idea to start with some of our tutorials before diving right into your specific use case. Let me know if there is anything else I can do to help.

1 Like

Thanks for your reply @Eric_Koleda. That’s helpful. I will try to learn basics and go on to implement step-by-step. I will definitely gonna need community’s help. Maybe I will ask one of the most dumb questions but I know that would be fine to ask here :smiley:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.