I keep getting to the point where I have no errors, but its still not working, I need help
// Personal Access Token
const token = 'pathr64Bev1PpaiJH.90e1e914f174f02f52979575ca3b30053bf32953f6727af49891b97a2a0c6f57';
// Base and Table Information
const baseId = 'appNQ88GsYacjfeij'; // Your Airtable base ID
const sourceTable = 'tblho4tbUAGTxgcB4'; // Replace with the source table name
const destinationTable = 'tbl596fVOoOn3g2y2';// Replace with the destination table name
// Fetch function for making API requests
async function fetchAirtableData(url) {
try {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(`Failed to fetch data from Airtable. Status: ${response.status} ${response.statusText}`);
}
return response.json();
} catch (error) {
throw new Error(`Failed to fetch data from Airtable: ${error.message}`);
}
}
// Main function
async function main() {
try {
console.log('Fetching records from the source table...');
const sourceUrl = `https://api.airtable.com/v0/${baseId}/${sourceTable}`;
console.log(sourceUrl);
const sourceResponse = await fetchAirtableData(sourceUrl);
const records = sourceResponse.records;
console.log(`Fetched ${records.length} records from the source table.`);
// Insert records into the destination table
console.log('Inserting records into the destination table...');
const destinationUrl = `https://api.airtable.com/v0/${baseId}/${destinationTable}`;
const insertPromises = records.map(async (record, index) => {
console.log(`Inserting record ${index + 1} of ${records.length}`);
try {
const insertResponse = await fetch(destinationUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ fields: record.fields })
});
if (!insertResponse.ok) {
throw new Error(`Failed to insert record. Status: ${insertResponse.status} ${insertResponse.statusText}`);
}
console.log(`Record ${index + 1} inserted successfully.`);
} catch (error) {
console.error(`Failed to insert record ${index + 1}: ${error.message}`);
}
});
await Promise.all(insertPromises);
console.log('All records inserted successfully.');
} catch (error) {
console.error('An error occurred:', error.message);
}
}
main();