I’ve scoured around for appropriate answers, but a good few of the results were not Wix/Velo related, or just simply didn’t suit my specific situation in helping me understand the issue.
Question:
Is this a side-effect of outdated code? I feel like I’m answering my own question just by asking it-- the code clearly works in the old tutorial (2 years ago now).
It seems to think that I (or some line of code) is calling a refresh function, but I can’t find it anywhere. It’s simply not written out where I can identify it with my beginner eye.
What am I missing here?
Product:
Wix Editor
What are you trying to achieve:
I’ve been trying to replicate the results of two seperate tutorials in one page of code on my dynamic item pages:
-
Allow only logged in users to like and unlike a certain page, store that “like” data into a dataset, which then automatically displays their “likes” in a private member page. I’ve been unsuccessful in getting the buttons to properly work, or even understand what I can do to fix the current errors I’ve been getting.
-
Check and update how many views the dynamic item page has for a counter that can be used around the site. I’ve been successful in replicating this part.
What have you already tried:
I followed this tutorial and this tutorial, both have code snippets copy & pastable from the video description which mirror my own. – I’ve tried something that’s probably just plain wrong but I don’t know what I’m doing in all honesty, but the biggest flags i noticed were: $w('#likedButton').onClick((event) => {
is the onClick event handler i keep getting here, which is WAY different to the template from the old video.
Furthermore, there’s at least 6 repeated lines of UserError: datasetApi 'refresh' operation failed. Caused by DatasetError: The dataset didn't load yet. You need to call refresh inside the onReady for the dataset.
- in my Run console.
This feels like it should be a simple fix, but it serves to confuse me entirely.
The most frustrating part is that no error lines are displayed in the Run console, nor the main page code window. It tells me what the error is, but not where it happens in code, nor how to fix it.
Additional information:
Here’s the entire page code.
(Modified Snippets)
`/*Author: Walter Odibi
Title: Email Trigger */
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
//VARIABLES
let currentItem;
let user = wixUsers.currentUser;
//CHECK IF USER HAS A LIKE OR NOT ON THAT ITEM (TRIGGER)
$w.onReady(async function () {
currentItem = await $w(‘#dynamicDataset’).getCurrentItem();
checkLike();
countLikes();
$w(‘#loginMessage’).onClick(loginMessageClick);
});
//CHECK IF USER HAS A LIKE OR NOT ON THAT ITEM (FUNCTION)
async function checkLike() {
if (wixUsers.currentUser.loggedIn) {
let likeResult = await wixData.query("Likes")
.eq("likes", currentItem._id)
.eq("userId", user.id)
.find();
if (likeResult.items.length > 0) {
$w('#likedButton').show('fade', { duration: 100 });
$w('#notLikedButton').hide();
} else
$w('#notLikedButton').show('fade', { duration: 100 });
$w('#likedButton').hide();
} else {
$w('#notLikedButton').show('fade', { duration: 100 });
}
}
//LIKED ICON CLICK
export async function likedButton_click(event) {
if (user.loggedIn) {
await removeLike();
await countLikes();
}
}
//NOT LIKED ICON CLICK
export async function notLikedButton_click(event) {
if (user.loggedIn) {
await addLike();
await countLikes();
} else
$w(‘#loginMessage’).show();
}
//LOGIN MESSAGE
async function loginMessageClick() {
let options = { “mode”: “login” };
$w(‘#loginMessage’).hide();
await wixUsers.promptLogin(options);
}
//ADD LIKE
async function addLike() {
let likeItem = {
likes: currentItem._id,
userId: user.id
};
$w('#notLikedButton').hide('fade', { duration: 100 });
$w('#likedButton').show('fade', { duration: 100 });
let result = await wixData.insert("Likes", likeItem);
}
//REMOVE LIKE
async function removeLike() {
let likeListResult = await wixData.query(“dynamicDataset”)
.eq(“likes”, currentItem._id)
.eq(“userId”, user.id)
.find();
if (likeListResult.length > 0) {
$w('#notLikedButton').show('fade', { duration: 100 });
$w('#likedButton').hide('fade', { duration: 100 });
await wixData.remove("dynamicDataset", likeListResult.items[0]._id)
}
}
//COUNT LIKES
async function countLikes() {
wixData.query(“dynamicDataset”)
.eq(“likes”, currentItem._id)
.count()
.then((totalLikes) => {
$w(“#likeCounterText”).text = String(formatLike(totalLikes))
});
}
//FORMAT THE LIKES PER ZERO
function formatLike(like) {
if (like > 999 && like < 1000000) {
return ${(like/1000).toFixed(0)}K
;
} else if (like >= 1000000 && like < 1000000000) {
return ${(like / 1000000).toFixed(0)}M
;
} else if (like >= 1000000000) {
return ${(like / 1000000000).toFixed(0)}B
;
} else if (like < 900) {
return like;
}
}
//VIEW EVENT FUNCTION
const viewsEvent = function () {
$w("#dynamicDataset").onReady(() => {
const itemObj = $w("#dynamicDataset").getCurrentItem();
let totalViews = itemObj.views + 1;
$w("#dynamicDataset").setFieldValue("views", totalViews)
$w('#dynamicDataset').save();
$w("#viewCount").text = formatView(String($w("#dynamicDataset").getCurrentItem().views))
});
}
//RUN THE VIEW WHEN PAGE LOADS⌛
$w.onReady(function () {
setTimeout(viewsEvent, 100);
});
//FORMAT THE VIEWS PER ZERO
function formatView(view) {
if (view > 999 && view < 1000000) {
return ${(view/1000).toFixed(0)}K
;
} else if (view > 1000000) {
return ${(view / 1000000).toFixed(0)}M
;
} else if (view > 1000000000) {
return ${(view / 1000000000).toFixed(0)}B
;
} else if (view < 900) {
return view;
}
}
`