Hello,
I have a database of Tests and each one has a dynamic page where you can upvote for it.
The problem is that when two users are on the dynamic page of the Test and they are trying to upvote for the Test without refreshing the page before, only one user can vote because the other one does not get the new voting parameter.
For example two users (user1 and user2) are on the Test dynamic page and the voting parameter (rating) for the Test is 0.
user1 is upvoting for it and updating the value in the database to 1. And now user2 is trying to upvote as well. The value user2 sees, if the page is not refreshed, is 0. So therefore user2 updated the value to 1 as well.
In the end the value ends up being 1.
Any ideas how to fix it?
My code below:
export function upvote_click(event) {
$w("#testsDatasetDynamic"). refresh();
let test= $w("#testsDatasetDynamic"). getCurrentItem();
// I saved an array of all voters so users can't vote twice.
let upvoters = test.upvoters;
let userId = wixUsers. currentUser. id;
// canVote is a value that is determined in the beginning of the onReady() function
// by searching the Test's upvoters array
if(canVote) {
upvoters.push(userId);
item.upvoters = upvoters;
item. rating = item.rating + 1;
wixData. update("Test", item). then(() => {
$w("#testsDatasetDynamic"). refresh();
canVote = false;
});
}
}
Thanks.