Insert Data to 2 Collections From A Form

I’m having trouble sending data from a form submission to two different collections that are connected through a reference field.

For more context, my website is designed to showcase films submitted by local filmmakers.

I’m creating a form where filmmakers enter their film metadata, including the film credits. Since the number of film credits strongly varies from film to film, I’ve added them in an input repeater.

I now have two collections:

  1. metadataSubmission (to collect the film metadata)

  2. creditNames (to collect the film credits)

I am using a reference field in the metadataSubmission collection named “creditFullName” to connect the films to their credits in the creditNames collection.

For each new film submission, there will be one new row in the Metadata Submission collection. For each row in the Metadata Submission collection, there will be multiple rows in the Credit Names collection.

Here is the code I have for saving the data to the two collections:

import wixData from "wix-data";
import wixLocation from 'wix-location';

export function insertMetadataSubmission(creditFullName, $w){
 let metadataSubmission = {
 "filmTitle": $w("#filmTitle").value,
 "firstName": $w("#firstName").value,
 "lastName": $w("#lastName").value,
 "emailAddress": $w("#emailAddress").value,
 "filmRuntime": $w("#filmRuntime").value,
 "releaseYear": $w("#releaseYear").value,
 "linkToFilm": $w("#linkToFilm").value,
 "filmLogline": $w("#filmLogline").value,
 "creditFullName": creditFullName._id,
 "selectedGenre": $w("#selectedGenre").value,
 "additionalGenre": $w("#additionalGenre").value,
 "selectedMood": $w("#selectedMood").value,
 "additionalMood": $w("#additionalMood").value,
 "keywords": $w("#keywords").value,
 "bts1": $w("#bts1").value,
 "bts2": $w("#bts2").value,
 "bts3": $w("#bts3").value,
 "bts4": $w("#bts4").value,
 "bts5": $w("#bts5").value,
 "behindTheFilm": $w("#behindTheFilm").value,
 "promo1": $w("#promo1").value,
 "promo2": $w("#promo2").value,
 "promo3": $w("#promo3").value,
 "promo4": $w("#promo4").value,
 "promo5": $w("#promo5").value,
 "promo6": $w("#promo6").value,
 "promo7": $w("#promo7").value,
 "promo8": $w("#promo8").value,
 "promo9": $w("#promo9").value,
 "promo10": $w("#promo10").value,
 "suggestedHashtags": $w("#dogweight").value,
 "confirmationCheckmark": $w("#confirmationCheckmark").value,
 "status": "PENDING"
    };
 
    console.log("metadataSubmission to be saved" + JSON.stringify(metadataSubmission));
 
    wixData.insert("metadataSubmission", metadataSubmission)
    .then( (results) => {
 let metadataSubmission = results; //see item below
        console.log("metadataSubmission in db: " + JSON.stringify(metadataSubmission));
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );
}

export function submitButton_click(event, $w) {
 let creditNames = {
 "name": $w("#creditNameInput").value,
 "instagram": $w("#instagramInput").value,
 "creditType": $w("#creditsDropdown").value,
 "submitting": false
    };
 
    console.log("creditNames to be saved" + JSON.stringify(creditNames));
 
    wixData.insert("creditNames", creditNames)
    .then( (results) => {
 let creditFullName = results; //see item below
        console.log("creditNames in db: " + JSON.stringify(creditFullName));
        insertMetadataSubmission(creditFullName, $w);       
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );

    wixLocation.to("/thank-you-film-metadata"); 
}

I tried following this example: https://www.wix.com/corvid/forum/community-discussion/how-to-insert-a-data-in-two-collection-from-a-form

However, for some reason, the user input is not being saved in the collections.

I noticed an error that says, "metadataSubmission is already declared in the upper scope.”

Is this the issue? I’m new to coding - any suggestions on how to fix this?

Or is there a better solution for connecting the two collections?

Link to page: https://www.bayareafilmmakers-arthouse.com/film-metadata

Thank you!

@bayareafilmmakerarth

Since you already declared metadataSubmission with the let command in the upper part of the page code, at this point just re-assign the value of that variable rather than declaring it:

metadataSubmission  = results;

When submitting a post, please make sure to follow these guidelines . Please do not post in ALL CAPS - including titles.

I apologize! Thank you for reminding me - Just fixed it!

@anthonyb thank you! There are no more errors showing up in the code, but unfortunately my form input is still not being saved to the two collections.

Here is all the code on the page - do you notice anything missing?

import wixData from "wix-data";
import wixLocation from 'wix-location';

// credits repeater code

let credits = [];

$w.onReady(async function () {
 let resCredits = await wixData.query("Credits").find();
 let resItems = resCredits.items;
    credits = resItems.map((item) => { return { "label": item.title, "value": item.title } });
    $w("#creditsRepeater").onItemReady(($item, itemData, index) => {
        $item('#creditsDropdown').options = credits;
        $item('#creditsDropdown').value = itemData.creditType;
    });
})

export async function addCreditButton_click_1(event) {
 await $w('#creditNamesDataset').new();
 let item = $w("#creditNamesDataset").getCurrentItem();
    $w("#creditsRepeater").forEachItem(($item, itemData, index) => {
 if (item._id === itemData._id) {
            console.log("new", item._id);
            $item('#creditsDropdown').show();
            $item('#creditNameInput').show();
            $item('#instagramInput').show();
        }
    }); 
}

async function addNewOnEmpty() {
 if ($w("#creditNamesDataset").getTotalCount() === 0) {
 await $w('#creditNamesDataset').new();
 let item = $w("#creditNamesDataset").getCurrentItem();
        $w("#creditsRepeater").forItems([item._id], ($item, itemData, index) => {
            $item('#creditsDropdown').options = credits;
            $item('#creditsDropdown').value = itemData.creditType;
            $item('#creditsDropdown').show();
            $item('#creditNameInput').show();
            $item('#instagramInput').show();
        });
    }
}





// saving user input to creditNames collection and metadataSubmission collection (with "creditFullName" reference field in metadataSubmission connecting them)

export function insertMetadataSubmission(creditFullName, $w){
 let metadataSubmission = {
 "filmTitle": $w("#filmTitle").value,
 "firstName": $w("#firstName").value,
 "lastName": $w("#lastName").value,
 "emailAddress": $w("#emailAddress").value,
 "filmRuntime": $w("#filmRuntime").value,
 "releaseYear": $w("#releaseYear").value,
 "linkToFilm": $w("#linkToFilm").value,
 "filmLogline": $w("#filmLogline").value,
 "creditFullName": creditFullName._id,
 "selectedGenre": $w("#selectedGenre").value,
 "additionalGenre": $w("#additionalGenre").value,
 "selectedMood": $w("#selectedMood").value,
 "additionalMood": $w("#additionalMood").value,
 "keywords": $w("#keywords").value,
 "bts1": $w("#bts1").value,
 "bts2": $w("#bts2").value,
 "bts3": $w("#bts3").value,
 "bts4": $w("#bts4").value,
 "bts5": $w("#bts5").value,
 "behindTheFilm": $w("#behindTheFilm").value,
 "promo1": $w("#promo1").value,
 "promo2": $w("#promo2").value,
 "promo3": $w("#promo3").value,
 "promo4": $w("#promo4").value,
 "promo5": $w("#promo5").value,
 "promo6": $w("#promo6").value,
 "promo7": $w("#promo7").value,
 "promo8": $w("#promo8").value,
 "promo9": $w("#promo9").value,
 "promo10": $w("#promo10").value,
 "suggestedHashtags": $w("#dogweight").value,
 "confirmationCheckmark": $w("#confirmationCheckmark").value,
 "status": "PENDING"
    };
 
    console.log("metadataSubmission to be saved" + JSON.stringify(metadataSubmission));
 
    wixData.insert("metadataSubmission", metadataSubmission)
    .then( (results) => {
        metadataSubmission  = results; //see item below
        console.log("metadataSubmission in db: " + JSON.stringify(metadataSubmission));
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );
}


export function submitButton_click(event, $w) {
 let creditNames = {
 "name": $w("#creditNameInput").value,
 "instagram": $w("#instagramInput").value,
 "creditType": $w("#creditsDropdown").value,
 "submitting": false
    };
 
    console.log("creditNames to be saved" + JSON.stringify(creditNames));
 
    wixData.insert("creditNames", creditNames)
    .then( (results) => {
 let creditFullName = results; //see item below
        console.log("creditNames in db: " + JSON.stringify(creditFullName));
        insertMetadataSubmission(creditFullName, $w);       
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );

    wixLocation.to("/thank-you-film-metadata"); 
}

@bayareafilmmakerarth No, I don’t notice anything missing, but this is a lot of code to sift through. So, is your console.log showing anything after the inserts? You might open a debug window to see if any errors are occurring that end up preventing all of the code from executing.