Hello, I’m attempting to make a data collection for a voting system. I have a collection (poll_movies) with the fields Title (text) and Votes (number). I’ve read some forum posts, and managed to get this piece of code (This is my first time actually coding JS). The fields are displayed in a repeater, thus giving each field their own button for voting.
Now, when I click on the vote button, the problem is that regardless of which fields’ button I click, the first field in the collection increments, and only that one.
I’m sure there’s a fairly obvious solution to this, but I’m really pulling my hair out here. Any help would be greatly appreciated.
The code is from here .
import wixData from ‘wix-data’ ;
import { session } from ‘wix-storage’ ;
export function ButtonVoteClick ( event ) {
wixData . query ( “poll_movies” ). find (). then (( result )=>{
let answersitem = result . items [ 0 ]; //item with values
let id = answersitem . _id ; // this will be used for
//writing data back
answersitem . votes ++; // increment field with votes by 1
// write updated value back to collection
wixData . update ( "poll_movies" , answersitem ). then (()=>{
// refresh dataset to show new value
//(this should be done in promise,
//after updating collection)
$w ( "#dataset1" ). refresh ();
});
}) ;
}
$w . onReady ( function () {
});