Need help with custom forms and collections

I am trying to create a form, which automatically feeds data to a collection. This is a custom form because I was using a pre-existing collection and I am using code to send data and it’s working fine in the preview, but when I publish the website, it’s not working.

This is the code:

import wixData from 'wix-data';

export function button1_click(event) {

let toInsert = {
    "sessionDate":$w('#date').value,
    "title":$w('#topicbox').value,
    "subject":$w('#subjectdrop').value,
    "startTime":$w('#start').value,
    "endTime":$w('#end').value,
    "aboutTheCourse":$w('#description').value,
    "shortCourseDescription":$w('#shortdescription').value,
    };

    let req1 = $w("#date").valid;
    let req2 = $w("#topicbox").valid;
    let req3 = $w("#subjectdrop").valid;
    let req4 = $w("#start").valid;
    let req5 = $w("#end").valid;
    let req6 = $w("#description").valid;
    let req7 = $w("#shortdescription").valid;
    
    if (req1 === false || req2 === false || req3 === false || req4 === false || req5 === false || req6 === false || req7 === false ) {
        $w('#errorText').text = "A required field* was left empty, please re-enter.";
        $w('#errorText').expand();
    } 
    else {
        wixData.insert("Courses1", toInsert)
        .then((results)=>{
            $w('#msg').text = "Your Session has been saved! Happy tutoring :)";
            $w('#msg').expand();
            $w('#errorText').collapse();
        })
        .catch((err)=>{
            $w('#msg').text = "There was an error, please resubmit.";
            $w('#msg').expand();
        });
    }
}

Whenever I press the submit button in the preview, the textbox #msg changes to “Your session has been saved”, but when I do on the website, it says “There was an error”, which means the issue is after the code has entered the if loop.

I would be very grateful for any insight on what I am doing wrong. By the way, I once converted this page to a “members-only” page because I only want members with certain roles to be able to view and submit this form, but I have changed it back to “everyone” and published the website, but it isnt working :frowning:

Can you console.error() your error and share it? Perhaps it’s a collections permissions issue?