[SOLVED] View count and Like count not updating in collection - code stopped working

Hello,
I’ve used code from the keynote speech about view counts to create a customised Like counter and Views counter on a dynamic page displaying character profiles from a book.
This all worked just fine the first time I tested it on the live site - the like button refreshed to show an additional like, the collection updated the right column, everything was smooth.
Then I added some social media share code, which worked… but now the Likes and Views counter code doesn’t seem to work anymore!
I’ve removed the social media share code and kept only the counter code, but no luck.

I am using a dynamic dataset called #dynamicDataset
My collection column for Likes is called charLikes
My collection column for Views is called charViews
I also have a collection column called characterReport working similarly to the Likes counter

Here is my code below:

import wixData from 'wix-data';

$w.onReady(function () {
 //TODO: write your page related code here...

});

//Voting and Viewing code
export function ifNull(a) {
 return (a === undefined) ? 0 : a;
}

var currentItem, ds;

export function dynamicDataset_ready(event) {
  ds = $w('#dynamicDataset');
  currentItem = ds.getCurrentItem();

 //Increment the number of views on this item
 let views = ifNull(currentItem.charViews) + 1;

 //In case the number of votes is null, set it to zero
 let votes = ifNull(currentItem.charLikes);

 //In case the number of votes is null, set it to zero
 let report = ifNull(currentItem.characterReport);

  ds.setFieldValue('charViews', views);
  ds.setFieldValue('charLikes', votes);
  ds.setFieldValue('characterReport', report);
  ds.save();
}

export function LikeHeartGrey_click(event) {
 //Add your code for this event here: 
  $w('#LikeHeartGrey').hide();

 //increment the number of votes and save it
 let votes = ifNull(currentItem.charLikes) + 1;
  ds.setFieldValue('charLikes', votes);
  ds.save()

}

export function ReportGrey_click(event) {
 //Add your code for this event here: 
  $w('#ReportGrey').hide();
  $w('#ReportTextPurple').show();

 //increment the number of votes and save it
 let report = ifNull(currentItem.characterReport) + 1;
  ds.setFieldValue('characterReport', report);
  ds.save()

}

Any thoughts would be very appreciated!

1 Like

Nicolas, as far as I can see, this code does not do anything but this:

var currentItem, ds;

The rest are functions that are never being called. Are you sure you did not delete (by mistake) some code in:

$w.onReady(function () {

Haha, so the real problem was, I had to set the database collection (not the dataset) to let ‘Anyone’ update content, not just ‘Site Members’ - this lets the viewcount and like counter update whenever anyone views/clicks on the buttons, not just the owner/member logged in.

This way, you don’t need to log in to ‘Like’ an asset that you’re viewing.

Hope this helps.