Dynamic Page View Count (Fixed, Still need help)

UPDATE: I found the problem, my database permissions were set to admin for updating of database content. I want the video to only be updated by the Admin though so if anyone has any loophole solutions I would appreciate them. I think I have to make a separate database to log the views and comments then, maybe “VideoAnalytics”?


So I am working on making a platform for video publishing. I’m at the stage where the item in the database (video) collects a view count. I have the code set up on the page, and It works fine in Editor Preview mode, however on the live site, it fails to record the page views into the item of the database. Any advice?

import wixLocation from 'wix-location';

$w.onReady(function () {

    $w("#videodataset").onReady( () => {

        setTimeout(laggedEvents,1000);

function laggedEvents() {

 const currentItem = $w("#videodataset").getCurrentItem();
 let viewCount =  currentItem.views + 1;
    $w("#videodataset").setFieldValue("views", viewCount);
    $w('#videodataset').save();
}
});
});
1 Like

If you want to keep the permissions as "Admin only’ and you want to make an exception and let a regular visitor to make a certain update. Then you should create a function in the backed with suppressAuth in specific conditions:
https://www.wix.com/corvid/reference/wix-data.html#WixDataOptions
https://www.wix.com/corvid/reference/wix-data.html#update

But in any case, you code should be run in the backend, because the you’re doing it now overrides many views of other visitors.