Liking system on a repeater

I’m new to code, I use a code from a partner here in VELO. Hope you can help me with the debug of the code or a better way to do it. Thanks! Problem line: 8 setData it says unexpected token. I think it may be something about this : // …Create and populate your repeater. Then within the onItemReady have the following code. But I don’t know how to do it.

//
let likes_array = [];

$w.onReady( function () {
setData();
});

HERE— setData() {

HERE— // …Create and populate your repeater. Then within the onItemReady have the following code

// Check if any likes exist, parse the JSON and count how many users have liked each post.
let likes = itemData.likes;
if (likes) {
console.log(likes);
let parsed_likes = JSON.parse(likes);
$w(“#likes”).text = parsed_likes.length.toLocaleString();
} else {
$w(“#likes”).text = (0).toLocaleString();
}

// For items that have likes, check to see if the current User was one of the likes, if so make heart full
if (itemData.likes) {
if (itemData.likes.includes( wixUsers.currentUser.id )) {
$w(“#emptyHeart”).hide();
$w(“#emptyHeart”).collapse();
$w(“#fullHeart”).show();
$w(“#fullHeart”).expand();
}
}

// If a user is not logged in, prompt login before they can like anything
// Each time a heart is “toggled” run function to update collection.
$w(“#emptyHeart”).onClick( (event) => {
if (wixUsers.currentUser.loggedIn) {
let $item = $w.at(event.context);
// let data_item = itemData;
toggleHeart($item, itemData);
} else {
logIn();
}
} );

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

let $item = $w.at(event.context);
// let data_item =
toggleHeart($item, itemData);
} );

// When empty heart is clicked, make it full and vise versa.
// Update the count by one depending on whether they like or unlike a post.
// Run the final functions which either add or remove a userId from the JSON.
async function toggleHeart(item, itemData) {

let emptyHeart = item(“#emptyHeart”);
let fullHeart = item(“#fullHeart”);
let likes = item(“#likes”);
// toggle the fold at the index
if (fullHeart.collapsed) {
fullHeart.expand();
fullHeart.show();
emptyHeart.collapse();
emptyHeart.hide();
// await fullHeart.disable();
await saveHeart(itemData);
// fullHeart.enable();
likes.text = (Number(likes.text) + 1).toLocaleString();
}
else {
fullHeart.collapse();
fullHeart.hide();
emptyHeart.expand();
emptyHeart.show();
// await emptyHeart.disable();
await deleteHeart(itemData);
// fullHeart.enable();
likes.text = (Number(likes.text) - 1).toLocaleString();
}

}

async function saveHeart(itemData) {

if (!itemData.likes) {
likes_array.push({
“_id”: wixUsers.currentUser.id
})
console.log(likes_array);
itemData.likes = await JSON.stringify(likes_array);
wixData.update(“Project_Submission”, itemData)
.then( (results) => {
});
} else {
likes_array = JSON.parse(itemData.likes);
likes_array.push({
“_id”: wixUsers.currentUser.id
})
console.log(likes_array);
itemData.likes = await JSON.stringify(likes_array);
wixData.update(“Project_Submission”, itemData)
.then( (results) => {
});
}

}

async function deleteHeart(itemData) {
console.log(“Delete Heart”);
if (itemData.likes) {
let parsed_likes = JSON.parse(itemData.likes);
let deleted_likes = parsed_likes.filter( function ( obj ) {
return obj._id !== wixUsers.currentUser.id ;

});
console.log(deleted_likes);
itemData.likes = await JSON.stringify(deleted_likes);
wixData.update(“Project_Submission”, itemData)
.then( (results) => {
// console.log()
});
}

}

Unexpected token —> means —> that you have probably forget a brace → ) (
or a curly-brace → } { somewhere in your CODE.

Or you have even one of theses braces too much.

Because of such errors, it is recommended to give his own code a good readable structure … like …

Bad code-structure (not good readable, especialy for beginners).

$w.onReady(()=>{
if () { 

}
else { 

}
})

Good readable structure (especially for beginners).

$w.onReady(()=>{
    if () { 

    }
    else { 

    }
})

Always when you open a brace, you should also close it!!!
{ <— open / close ----> }
( <— open / close ----> )
[ <— open / close ----> ]

And next time, please use CODE-TAGs to show your code in the right good readable structure like in my given examples.

Good luck and happy coding.:wink:

Great! Ill do it that way, hope ill find it. Thanks.

@camperodi11
A lot of failures! Absolutely no coding structure!

// 
let likes_array = [];
 
$w.onReady(function () {
 setData();
});

HERE--- setData() {
 
HERE--- // .....Create and populate your repeater. Then within the onItemReady have the following code

// Check if any likes exist, parse the JSON and count how many users have liked each post. 
let likes = itemData.likes;                    
 if (likes) {
                        console.log(likes);
 let parsed_likes = JSON.parse(likes);
                        $w("#likes").text = parsed_likes.length.toLocaleString();
                    } else {
                        $w("#likes").text = (0).toLocaleString();
                    }

// For items that have likes, check to see if the current User was one of the likes, if so make heart full
if (itemData.likes) {
 if (itemData.likes.includes(wixUsers.currentUser.id)) {
            $w("#emptyHeart").hide();
            $w("#emptyHeart").collapse();
            $w("#fullHeart").show();
            $w("#fullHeart").expand();
            }
          }



// If a user is not logged in, prompt login before they can like anything
// Each time a heart is "toggled" run function to update collection. 
$w("#emptyHeart").onClick( (event) => {
 if (wixUsers.currentUser.loggedIn) {
 let $item = $w.at(event.context);
 // let data_item = itemData;
                        toggleHeart($item, itemData);
                        } else {
                            logIn();
                        }             
                    } );                                                                    
 
                    $w("#fullHeart").onClick( (event) => {
 let $item = $w.at(event.context);
 // let data_item = 
                        toggleHeart($item, itemData);                     
                    } );


// When empty heart is clicked, make it full and vise versa.
// Update the count by one depending on whether they like or unlike a post.
// Run the final functions which either add or remove a userId from the JSON.  
async function toggleHeart(item, itemData) {

 let emptyHeart = item("#emptyHeart");
 let fullHeart = item("#fullHeart");
 let likes = item("#likes");
 // toggle the fold at the index
 if (fullHeart.collapsed) {
        fullHeart.expand();
        fullHeart.show();
        emptyHeart.collapse();
        emptyHeart.hide();
 // await fullHeart.disable();
 await saveHeart(itemData);
 // fullHeart.enable();
        likes.text = (Number(likes.text) + 1).toLocaleString();
    }
 else {
        fullHeart.collapse();
        fullHeart.hide();
        emptyHeart.expand();
        emptyHeart.show();
 // await emptyHeart.disable();
 await deleteHeart(itemData);
 // fullHeart.enable();
        likes.text = (Number(likes.text) - 1).toLocaleString();
    }
 
}

async function saveHeart(itemData) {


if (!itemData.likes) {
        likes_array.push({
 "_id": wixUsers.currentUser.id
        })
        console.log(likes_array);
        itemData.likes = await JSON.stringify(likes_array);
        wixData.update("Project_Submission", itemData)
        .then( (results) => {
        });
    } else {
        likes_array = JSON.parse(itemData.likes);
        likes_array.push({
 "_id": wixUsers.currentUser.id
        })
        console.log(likes_array);
        itemData.likes = await JSON.stringify(likes_array);
        wixData.update("Project_Submission", itemData)
        .then( (results) => {
        });
    }


}

async function deleteHeart(itemData) {
    console.log("Delete Heart");
 if (itemData.likes) {
 let parsed_likes = JSON.parse(itemData.likes);
 let deleted_likes = parsed_likes.filter(function( obj ) {
 return obj._id !== wixUsers.currentUser.id;
 
});
    console.log(deleted_likes);
        itemData.likes = await JSON.stringify(deleted_likes); 
        wixData.update("Project_Submission", itemData)
        .then( (results) => {
 // console.log()
        });
    }


}

CORRECTION…

// 
let likes_array = [];
 
$w.onReady(function () {
    setData();

 // If a user is not logged in, prompt login before they can like anything
 // Each time a heart is "toggled" run function to update collection. 
    $w("#emptyHeart").onClick( (event) => {
 if (wixUsers.currentUser.loggedIn) {
 let $item = $w.at(event.context);
 // let data_item = itemData;
            toggleHeart($item, itemData);
        } 
 else {logIn();}
    } );                                                                    
 
    $w("#fullHeart").onClick( (event) => {
 let $item = $w.at(event.context);
 // let data_item = 
            toggleHeart($item, itemData);                     
        } );
});

setData() {
 let likes = itemData.likes;                    
 if (likes) {console.log(likes);
 let parsed_likes = JSON.parse(likes);
        $w("#likes").text = parsed_likes.length.toLocaleString();
    } 
 else {$w("#likes").text = (0).toLocaleString();}

 // For items that have likes, check to see if the current User was one of the likes, if so make heart full
 if (itemData.likes) {
 if (itemData.likes.includes(wixUsers.currentUser.id)) {
            $w("#emptyHeart").hide();
            $w("#emptyHeart").collapse();
            $w("#fullHeart").show();
            $w("#fullHeart").expand();
        }
    }
})

 
// When empty heart is clicked, make it full and vise versa.
// Update the count by one depending on whether they like or unlike a post.
// Run the final functions which either add or remove a userId from the JSON.  
async function toggleHeart(item, itemData) {
 let emptyHeart = item("#emptyHeart");
 let fullHeart = item("#fullHeart");
 let likes = item("#likes");
 // toggle the fold at the index
 if (fullHeart.collapsed) {
        fullHeart.expand();
        fullHeart.show();
        emptyHeart.collapse();
        emptyHeart.hide();
 // await fullHeart.disable();
 await saveHeart(itemData);
 // fullHeart.enable();
        likes.text = (Number(likes.text) + 1).toLocaleString();
    }
 else {
        fullHeart.collapse();
        fullHeart.hide();
        emptyHeart.expand();
        emptyHeart.show();
 //  await emptyHeart.disable();
 await deleteHeart(itemData);
 //  fullHeart.enable();
        likes.text = (Number(likes.text) - 1).toLocaleString();
    }
}

async function saveHeart(itemData) {
 if (!itemData.likes) {
        likes_array.push({
 "_id": wixUsers.currentUser.id
    })
    console.log(likes_array);
    itemData.likes = await JSON.stringify(likes_array);
    wixData.update("Project_Submission", itemData)
    .then( (results) => { });
    } 
 else {
        likes_array = JSON.parse(itemData.likes);
        likes_array.push({
 "_id": wixUsers.currentUser.id
    })
    console.log(likes_array);
    itemData.likes = await JSON.stringify(likes_array);
    wixData.update("Project_Submission", itemData)
    .then( (results) => { });
    }
}

async function deleteHeart(itemData) {
    console.log("Delete Heart");
 if (itemData.likes) {
 let parsed_likes = JSON.parse(itemData.likes);
 let deleted_likes = parsed_likes.filter(function( obj ) {
 return obj._id !== wixUsers.currentUser.id;
        });
        console.log(deleted_likes);
        itemData.likes = await JSON.stringify(deleted_likes); 
        wixData.update("Project_Submission", itemData)
        .then( (results) => {   });
    }
}

Ok ok great, awesome orientation from u! Im struggling a lot. But thank u very much.

You are welcome.

@russian-dima можно как то получить от тебя пару консультаций для начинающего?)

@huyovayalozovaya
Конечно вы можете. Какие у вас вопросы?
Откройте новый пост, посвященный вашей проблеме, и напишите проблему.
Если вам нужна личная консультация, вы можете заглянуть в мой ПРОФИЛЬ, там вы найдете дополнительную информацию обо мне.

Of course you can. What are your questions?
Open a new post, with your own issue and write down the problem.
If you want a personal consultations, you can take a look into my PROFILE, there you will find some more infos about me.