Save my repeater item once

Hi,

I have a like heart on my repeater, I want if the logged in user clicks on the heart then it logs it to my collection. The code I have works just fine, it’s just that it logs the same information for the amount of items in my collection. i.e. If I have three items in my collection, then the information save three times. please help:

export async function emptyHeart_click(event) {

$w("#repeater1").forEachItem ( ($item, itemData, index) => { 

let user = wixUsers.currentUser;
let userId = user.id;

if (wixUsers.currentUser.loggedIn) {

const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.

    wixData.query("Talk_Topic_Request") // get the item from the database collection. 
    .eq("_id", itemId) 
    .find() 
    .then((results) => { 

let topicId = results.items[0]._id;
let title = results.items[0].title;

function randomNumber (len) {
var x;
var n = ‘’;
for(var count = 0; count < len; count++) {
randomNumber = Math.floor(Math.random() * 10 );
n += randomNumber.toString();
}
return n;
}

let pluslike = “Like” + randomNumber(2);

let newlike = {
“title”: title,
“titleId”: itemId,
“likes”: 1,
“userId”: userId,
};

         console.log(newlike) 

         wixData.insert("Likes", newlike); 


        CountLikes(); 

let $btn = $w.at(event.context);

        $btn('#fullHeart').expand(); 

        $btn('#emptyHeart').collapse(); 
    });   



 }  **else**  { 
     wixUsers.promptLogin() 
 } 

}) 

}

1 Like