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));