Help

Re: Adding all the data from one table to another with Javascript

Solved
Jump to Solution
1449 0
cancel
Showing results for 
Search instead for 
Did you mean: 
shenson
4 - Data Explorer
4 - Data Explorer

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



1 Solution

Accepted Solutions
Ron_Williams
6 - Interface Innovator
6 - Interface Innovator

@shenson

My guess would be something to do with the types of fields you are trying to import and export. This will work with all text fields, but attachments, for example, return an array, which will need to be parsed for the url. 

I would suggest:

console.log("RECORDS", records);
after
console.log(`Fetched ${records.length} records from the source table.`);
This way you can see what the incoming record looks like. If there is an array in there, you will need to extract the url and reconstruct it like this:
"Image_1": [{
"url": url
}],
Ive also had issues in the past with null-where I had to construct if statements if the field was null it skipped it. 
You could also create a new view, add the "viwXXX" to the fetch url, and add fields into that view until you find the trouble field. 
 
Hope that helps.

 

See Solution in Thread

1 Reply 1
Ron_Williams
6 - Interface Innovator
6 - Interface Innovator

@shenson

My guess would be something to do with the types of fields you are trying to import and export. This will work with all text fields, but attachments, for example, return an array, which will need to be parsed for the url. 

I would suggest:

console.log("RECORDS", records);
after
console.log(`Fetched ${records.length} records from the source table.`);
This way you can see what the incoming record looks like. If there is an array in there, you will need to extract the url and reconstruct it like this:
"Image_1": [{
"url": url
}],
Ive also had issues in the past with null-where I had to construct if statements if the field was null it skipped it. 
You could also create a new view, add the "viwXXX" to the fetch url, and add fields into that view until you find the trouble field. 
 
Hope that helps.