Hello everybody,
I am moving my first steps with Corvid as i am developing a simple social network with posting and like system. It has a bunch of problems i need to solve, in this post i will just mention one that is bugging me…
i need to implement the Like count. I mean, when somebody likes on a post via an icon, i want the number of likes to be shown near the icon.
Here is how my page looks like in the Editor:
So, everything has been connected via the datasets.
The Like blue button is called Like , the Unlike red button is called Liked (and is hidden on load), the database is called Likes , ad the dataset is called Likes as well. The dataset, I have defined it as Read and Write , while the database contains a field called numberOfLikes set as a number, where i want to write to / gather from the number of likes for each post, that would be shown in the connected text between the 2 colored buttons (placeholder text: Likes).
The full code of the image Like_Click event is like this:
export function Like_click(event) {
wixData.insert('Likes',
{likedPost: $w('#userPosts').getCurrentItem()._id}),
$w('#Liked').show(),
$w('#Like').hide();
wixData.query("Likes").find().then((result)=>{
let likesitem = result.items[0];
let id = likesitem._id;
likesitem.numberOfLikes++;
wixData.update("Likes", likesitem).then(()=>{
$w("#Likes").refresh();
});
}) ;
}
Now, ignoring the part about userPosts, the actions to be accomplished by the click event are:
- writing the post in a textfield (likedPost) in the Likes database via the Likes dataset
- hiding the Like blue button
- showing the red Liked button (to allow ‘Unlike’)
- incrementing the value of the number in the database field numberOfLikes
- retrieve the number just written in the numberOfLikes field in the Database and show it in the connected text between the 2 buttons.
Everything is perfect in the first 3 points, but the 2 last points don’t work, and i cannot figure out why… I don’t get any error, simply the number does not show up.
Looking at the Likes Database content, I can see that the numberOfLikes field has been updated, but it looks like every click on the Like button created a new record instead of updating the same one.
Anybody please can help? I thank you in advance, please e patient as i am a complete beginner with Corvid.
Alessandro
EDIT: sorry, I posted this in the Tips section i did not realize this was the correct section of the forum.