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();
}
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.
//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()