API : Update associated collections

Hi,

I’m having a few challenges with the API. I can add records and fetch records without an issue but I can’t see to associate two collections.

I have a Planes collection and a Flights collection. A Plane can have many flights, a flight can only have one plane.

I’m programmatically creating a 'Flight" which is successful, I can see it in the collection. I get the returned ID of the new flight record. I am then using the new flight record ID to UPDATE a Plane. It returns successfull but doesn’t actually work. What am I doing wrong?

Some snippets: (in JavaScript)

Saving the Flight record - works fine

// Save flight data to Adalo database
        const adaloEndpoint = <hidden>';
        const response = await fetch(adaloEndpoint, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${adaloToken}`
            },
            body: JSON.stringify(flightDataJSON)
        });

        if (!response.ok) {
            throw new Error('Failed to save flight data to Adalo');
        } else {
            const responseData = await response.json(); // Parse response JSON
            createdFlightRecordID = responseData.id; // Assuming Adalo returns the ID in the response
            updateMessage(`Saved flight data successfully. Record ID: ${createdFlightRecordID}`);
            await updateModelwithFlightIDInDatabase();
        }

Using the flight record ID to update the Plane with the Flight

// Update the Flights array with the new flight record ID
        var tempcreatedFlightRecordID = Number(createdFlightRecordID);

        // Construct the JSON payload with the updated Flights property
        const modelDataJSON = {
            "Flights": tempcreatedFlightRecordID
        };

        console.log('Updated Model Data JSON:', modelDataJSON); // Log the updated model data

        // Make a PUT request to update the model in Adalo
        const updateResponse = await fetch(adaloUpdateEndpoint, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${adaloToken}`
            },
            body: JSON.stringify(modelDataJSON)
        });

        if (!updateResponse.ok) {
            throw new Error('Failed to update model in Adalo');
        }else{
            const updatedModelData = await updateResponse.json();
            console.log('Updated Model Data:', updatedModelData); // Log the updated model data
            
            updateMessage(`Successfully updated Model`);
        }

API JSON scheme, Flights is Number


"Flights": [
1,
2,
3
],

I just managed to get this working.

I didn’t need to update the Planes id. I instead pushed the planeID into the flight ID, even though the API documentation doesn’t list it

flightDataJSON = {
            "Name": "Imported from Flight Logger " + flightLoggerName,
            "Model": tempModelID,
          ...
        };

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