View Count not working

Please help! I am trying to integrate a view count, but the data (counts) are not registering inside the collection.

This is the code i am using, which seems fine, but when i view the page and go to view the collection, the data isn’t counting the view. Why?

import {ifNull} from 'public/utils.js';
var currentItem, ds;

export function dynamicDataset_onready() {

	ds = $w('#dynamicDataset');
	currentItem = ds.getCurrentItem();

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

	ds.setFieldValue('viewCount', views);
	ds.save();
}

Hi Robert, is it not working in the published site or in preview as well? Are there any errors in the developer console?

Hi Tomer,

This is what i am seeing - anything?

There is indeed an issue there with importing the utils.js file from the public folder. Can you please post a link to your site so I can take a look?

Did you verify that you have a file called utils.js under the public folder?

No - i don’t, do i need one :o what do i put in it?

You import this file (first line in your code) so I assumed it’s a file you created and maybe deleted. Did you copy this code from somewhere else?

I watched the beta launch video (Uval) demonstrating the product. So how do i create the public file?

You don’t have to, you can implement the “ifNull” function in the same file. What it basically does is it makes sure the view count is 0 in case it was never defined in the database.

Hi Tomer,
I¨m having the same problem.
So you says I can just delete “ifNull” from the code?

So it will look like this?

var currentItem, ds;

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

let views = (currentItem.viewCount) +1;
let votes = (currentItem.voteCount);	

ds.setFieldValue('viewCount', views);
ds.setFieldValue('voteCount', votes);
ds.save();	
	
}	
	
export function heart_onClick(event, $w) {
	$w('#heart').hide();
	$w('#thanksBox').show('SLideIn');
	
	let votes = (currentItem.voteCount) +1;
ds.setFieldValue('voteCount', votes);
ds.save();

Problem with this code is that the view and vote count doesn’t show us.

This is my test site:

https://kubanjakub91.wixsite.com/goa-test/Novinky/Chef

For some reason the View Count increments by 2 instead of 1. Any suggestions?

import wixData from ‘wix-data’;

//Voting and Viewing code

export function ifNull(a){
return (a===undefined) ? 0 : a;
}

var currentItem, ds;

export function ratingsDataset_ready() {
ds = $w (‘#ratingsDataset’);
currentItem = ds.getCurrentItem();

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

//In case the number of votes is null, set it to zero 
let votes = ifNull(currentItem.voteCount); 
ds.setFieldValue('viewCount', views); 
ds.setFieldValue('voteCount', votes); 
ds.save(); 

}

export function vectorHeart_click(event) {
$w(‘#vectorHeart’).hide();
$w(‘#vectorHeartRed’).show();

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

}