Code is working in preview mode but not working in published mode

Hello, I am very new to coding but with the help of this forum and corvid reference api, I was able to write some code. My code’s intention is to :-

  1. Collect rating in a particular page of my website and store it in a database “BJPRating”. This rating is between 1 and 10 and so I am not using the built-in input rating by wix. I have used 10 star icon button and a submit button to achieve this. And also only registered members on the website can submit their rating. One user can submit his rating only once, i.e. when he submits his rating it is stored in the “BJPRating” databse and so he cannot rate this item more than once, but he can change his rating if he wants. Hence the code is as follows:
const forsubmit = function () { 
 
 let userOwner = wixUsers.currentUser.id;         //wix user is imported
    console.log(userOwner);
    wixData.query("BJPRating")                     //wix data is imported
        .eq('_owner', userOwner)
        .find()
        .then((result) => {
             if (result.items.length > 0) {
                console.log(result);
             let abc = result.items[0];
             let itemId = abc._id;
                console.log(itemId);
                wixData.get("BJPRating", itemId)
                    .then((item) => {
                        item.userRating = a;    // "a" is the Rating the user had selected
                                                // between 1 and 10
                        wixData.update("BJPRating", item)
                            .then(() => {
                                Sum_amount();
                                Total_count();
                            });
                    });
            } else {
                 let toInsert = {
                 "userRating": a,
                    };
                    wixData.insert("BJPRating", toInsert)
                    .then((results) => {
                     let item = results; //see item below
                        console.log("***Updated");
                        Sum_amount();
                        Total_count();
                    })
                    .catch((err) => {
                     let errorMsg = err;
                    });
                }
        })
        .catch((error) => {
            console.log(error.message);
        });
    Sum_amount(); //Function to calculate Average of all the ratings
    Total_count(); // Function to calculate Total number of ratings submitted
}

export function button1_click(event) {

 //Add your code for this event here: 
 if (wixUsers.currentUser.loggedIn) {
        console.log(wixUsers.currentUser.id)
        forsubmit();
        Sum_amount();
        Total_count();
    } else {
        wixWindow.openLightbox("Sign in all");
 
	2. From the database "BJPRating" , I calculate the total number of ratings and the average			    	rating using corvid and display it on "Textbox1" & "TextBox2"

Now the code for (2) is working perfectly. But the code for (1) is working in the preview mode but not in the published mode. I have already contacted wix support team, but they were unable to solve this issue.
Can anyone please help me out in finding the issue in the code? If there are other better way to achieve my points please suggest those as well.

Hello bonnydeory,

well first at all i want to say, that your statement and the code do not fits.

Either you have got this code from another place, or you are not just a beginner in coding.

But anyway, did you already checked both databases for their permissions???

Put all permissions of both DATABASES to the lowest level-security for TESTING.

And also make sure, that your datasets, are setted correctly (READ & WRITE)-CHECK.

The fact, that your code works in “Preview-Mode” tells that your code seems to be ok and that you just do not have permissions to enter the database.
(on my opinion).

Thanks a lot russian-dima for the reply. Well I am a beginner in coding about 3 months now… And it was the database permissions… This idea just came to me about half hour before reading your comment, and this is the solution… Thanks a lot once again…