Hello!
I have a problem creating a wish list for my site. The thing is that once I click “Add to list”, nothing happens. I have two buttons and the first one is “Add to list” (it’s not linked) and the second one is “view list”. The second one is linked to a page called “My List”. Once an item has been added, there should be a text showing “Success! Item has been saved”, but nothing happens. Here is my code:
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
let user;
var userID;
let itemObj;
var product_name;
$w.onReady(function () {
$w(“#dynamicDataset”).onReady(() => {
product_name = $w(“#text31”).text;
});
user = wixUsers.currentUser;
userID = user.id;
let isLoggedIn = user.loggedIn;
if (isLoggedIn == true) {
$w("#dynamicDataset").onReady(() => {
testwish(product_name);
});
} else {
wixUsers.promptLogin()
}
});
function testwish() {
let userId = user.id;
wixData.query(“List”)
.eq(“title”, product_name)
.eq(“userId”, userId)
.find()
.then((results) => {
let count = results.totalCount;
if (count == 0) {
$w(“#button1”).label = “ADD TO WISH LIST”;
} else {
$w(“#button1”).label = “REMOVE FROM WISH LIST”;
}
});
}
function dothewish() {
let userId = user.id;
wixData.query(“List”)
.eq(“title”, product_name)
.eq(“userId”, userId)
.find()
.then((results) => {
let items = results.items;
let firstItem = items[0];
let count = results.totalCount;
if (count == 0) {
let toSave = {
“productId”: product_name,
“userId”: userId
};
wixData.save(“wishlist”, toSave)
$w(“#button1”).label = “REMOVE FROM WISH LIST”;
} else {
wixData.remove(“wishlist”, firstItem._id);
$w(“#button1”).label = “ADD TO WISH LIST”;
}
});
}
export function addtowishlist_onclick() {
dothewish(product_name);
}
Thanks!