Velo Voting System - Help!

We’re creating an online art gallery and we need the site visitors to vote for their favourite piece of artwork.

We’ve got a database setup that contains all of the artwork information with a number field called “Votes” which we need to count the total number of people who vote for that specific artwork.

On the page, we have a repeater connected to a dataset with a ‘Vote’ image/button and the code below:

The problem:
All works as expected HOWEVER, it always counts the vote against the first record in the database, not the ‘current item’ in the repeater.

Any help is greatly appreciated as I’m now pulling my hair out :rofl:

export function vectorImage1_click ( event ) {

**if** ( wixUsers . currentUser . loggedIn ) {  // if the user is logged in 

    **let**  currentVotes  =  		Number ( $w ( '#dataset1' ). getCurrentItem (). votes ); 
    
    $w ( "#dataset1" ). setFieldValue ( "Votes" ,  currentVotes  +  1 ); 
    $w ( "#dataset1" ). save (); 
    $w ( "#voteButton" ). hide (); 
    $w ( "#voteConf" ). show (); 
    $w ( "#text64" ). text  =  "One vote per e-mail address" ; 
} 

**else**  { // the user isn't logged in 

    $w ( "#voteConf" ). show (); 
    $w ( "#box1" ). hide (); 
    $w ( "#voteConf" ). text  =  " LOGIN REQUIRED TO VOTE " ; 
    wixUsers . promptLogin () 
    $w ( "#voteConf" ). hide (); 
    $w ( "#voteConf" ). text  =  " VOTE COUNTED! " ; 
    $w ( "#box1" ). show (); 

}
}

There’re some problems with your code.
But one of them is not a problem with the code itself but with the logics behind it:
In your case if several members navigate to the page at the same time and upvote an item, they will override each other. The solution for your code depends on whether or not you care about this problem (it’ll be a different code with different logics if you wish to avoid this problem).

Hi J. D.,

Thank you for reaching out - very much appreciated! We’ve actually been working on the code a little more and edited to this:

import wixUsers from ‘wix-users’ ;

$w . onReady ( function () {

$w ( "#voteButton" ). onClick ( ( event ) => { 

**let**  $item  =  $w . at ( event . context ); 

**if** ( wixUsers . currentUser . loggedIn ) {  // if the user is logged in 

    **let**  currentArtwork  =  $item ( "#dataset1" ). getCurrentItem (); 
    **let**  currentVotes  =  Number ( currentArtwork . Votes ); 
    $item ( "#dataset1" ). setFieldValue ( "Votes" ,  currentVotes  +  1 ); 
    $item ( "#dataset1" ). save (); 
    $w ( "#voteButton" ). hide (); 
    $w ( "#voteConf" ). show (); 
    $w ( "#text64" ). text  =  "One vote per e-mail address" ; 

} 

**else**  {  // the user isn't logged in 

    $w ( "#voteConf" ). show (); 
    $w ( "#box1" ). hide (); 
    $w ( "#voteConf" ). text  =  " LOGIN REQUIRED TO VOTE " ; 
    wixUsers . promptLogin () 
    $w ( "#voteConf" ). hide (); 
    $w ( "#voteConf" ). text  =  " VOTE COUNTED! " ; 
    $w ( "#box1" ). show (); 

} 

} );

});

Hi,

Do you mind if I ask if you managed to get this code working in the end? I’m trying to do something very similar!

Thanks,
tom.

Hey Tom,

We did indeed and we’ve created a video:

Wow, that is fantastic, thanks so much for sharing this! You’ve saved me a ton of headache! Really appreciate the quick reply and the info.

Just had a good look through, and that’s brillaint, but it doesn’t have the login process included to make sure people can ony vote once. Were you able to get those elements working?

Hi,

Thanks so much for the reply and your video, that was a huge help. Do you mind if I ask about the login part of it, as that doesn’t seem to be covered in the video, and it would be very useful to have to contrain people to only being able to vote once.

Thanks again,
tom.