Using wix the code works well in preview mode but the code doesn’t work after publishing

Facing problem in updating the “Request” database,
Kindly provide solution:
Provided the code below,

import wixData from ‘wix-data’;

export function reservation_click(event) {
// Fetch data from CartForm database
//$w(“#reservation”).onClick(() => {
wixData.query(“CartForm”)
.find()
.then((results) => {
if (results.items.length > 0) {
// Get the first item (assuming there’s only one)
const cartItem = results.items[0];
// Extract order status and order number
const orderStatus = cartItem.orderStatus;
const orderNumber = cartItem.orderNumber;

            // Get values from form input fields
            let name = $w('#name').value;
let email = $w('#email').value;
let code = $w('#countryCode').value;
let phone = $w('#phone').value;
let address = $w('#address').value;
let comments = $w('#comments').value;
let startDate = $w('#datePicker1').value;
let startTime = $w('#timePicker1').value;
let endDate = $w('#datePicker2').value;
let endTime = $w('#timePicker2').value;


console.log("Name: ", name);
console.log("Email: ", email);
console.log("Country Code: ", code);
console.log("Phone: ", phone);
console.log("Address: ", address);
console.log("Comments: ", comments);
console.log("Start Date: ", startDate);
console.log("Start Time: ", startTime);
console.log("End Date: ", endDate);
console.log("End Time: ", endTime);
// Get other form input values similarly…

            // Update Request database with collected data
            wixData.insert("Request", {
                name: name,
                email: email,
                countryCode: code,
                phoneNumber: phone,
                address: address,
                comments: comments,
                startDate: startDate,
                startingTime: startTime,
                endDate: endDate,
                endTime: endTime,
                orderNumber: orderNumber,
                orderStatus: 'Completed'

            })
            .then((results) => {
                // Handle successful database update
                console.log("Request added: ", results);
                // Optionally, show a success message or redirect to a thank you page
            })
            .catch((err) => {
 console.error("Error inserting into Request database: ", err);
                // Optionally, show an error message
            });
        } else {
            console.warn("No items found in CartForm database.");
            // Optionally, show a message to the user indicating no items were found
        }
    })
    .catch((err) => {
        // Handle error
        console.error("Error querying CartForm database: ", err);
        // Optionally, show an error message
    });

Preview runs as admin so you likely have a permission issue where regular users are unable to insert into this collection. You can read more about setting permissions here. Do note you’ll want to make sure that users cannot write to or read from other user data. Restricting reads/updates/adds to “Site Members” will help with this but you’ll need to configure according to your needs.

If you need more granular control over what exactly gets written, by who, and when they you may want to use Velo Web Modules: Calling Backend Code from the Frontend

Thank you for the solution, will try and get back.

1 Like