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 :-
- 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.