Data not saved to CMS when running website in publish mode

Question:
My input and dropdown data on screen is not submitting into my CMS collection. this works when I ‘preview’ and test it but as soon as I ‘publish’ and try nothing happens.
below is my code:

import wixData from ‘wix-data’;

$w.onReady(function () {

$w('#SubmitSale').onClick((event) => {
		var name = $w('#StrainDropdown').value
		var amount = $w('#StrainAmount').value
		var cash = $w('#StrainCash').value

		let toInsert = {
			"StrainName": name,
			"StrainAmount": Number(amount),
			"StrainCashAmount": Number(cash)
		}

	wixData.insert("Sales", toInsert)
	.then((item) => {
		console.log(item);
	})
	.catch((err) =>{
		console.log(err);
	})
	
	$w('#StrainDropdown').value = "";

	$w('#StrainAmount').value = "";

	$w('#StrainCash').value = "";

})

});

Product:
[Wix editor]

**What are you trying to achieve: **
On the submit button the data is supposed to save to the CMS collection. this works in preview mode but as soon as I run publish mode and do it nothing is saved into the CMS.

Hi Marius,
I added some error catching to your code,

$w.onReady(function () {
    $w('#SubmitSale').onClick((event) => {
        const name = $w('#StrainDropdown').value;
        const amount = $w('#StrainAmount').value;
        const cash = $w('#StrainCash').value;

        if (!name || !amount || !cash) {
            console.log("Missing required fields.");
            return;
        }

        const toInsert = {
            "StrainName": name,
            "StrainAmount": Number(amount),
            "StrainCashAmount": Number(cash)
        };

        console.log("Data to Insert:", toInsert);

        wixData.insert("Sales", toInsert)
            .then((item) => {
                console.log("Data Inserted Successfully:", item);
                // Clear fields after successful insertion
                $w('#StrainDropdown').value = "";
                $w('#StrainAmount').value = "";
                $w('#StrainCash').value = "";
            })
            .catch((err) => {
                console.error("Error Inserting Data:", err);
            });
    });
});

And how you have to check the dataset’s permission to which user can be insert the item into CMS like “anyone can add, update , delete”.