I’m trying to essentially make a ‘watchlist’ where each movie is a dynamic page (movieitem), the watchlist is a dynamic page (Watchlist) that references the movie page, but the way you add the movie to the watchlist is from a static page (using the dataset #moviedataset).
I feel like the way to do it is with line 12, {movieitem: $w( “#moviedataset” ).getItems( 1 )};
(“1” is the ID of the movie I’m trying to reference)
But it doesn’t get added, and in the live collection view it adds all the fields but the correct reference. Can anyone help with the mess I’m making?
import wixUsers from 'wix-users';
import wixData from 'wix-data';
let user;
var userID;
let itemObj;
var movie_name;
var movieitem;
$w.onReady(function () {
$w("#moviedataset").onReady(() => {
movie_name = $w("#text3").text,
{movieitem: $w("#moviedataset").getItems(1)};
});
user = wixUsers.currentUser;
userID = user.id;
let isLoggedIn = user.loggedIn;
if (isLoggedIn === true) {
$w("#moviedataset").onReady(() => {
testwatch(movie_name),
$w("#logintosave").hide();
});
} else {
$w("#logintosave").show();
}
});
async function removeFromWatchlist() {
let _owner = user.id,
wishListResult = await wixData.query("Watchlist")
.eq("title", movie_name)
.eq("movieitem", movieitem)
.eq("_owner", _owner)
.find();
if (wishListResult.length > 0) {
$w("#addtowatchlist").show(),
$w("#added").hide();
await wixData.remove("Watchlist", wishListResult.items[0]._id)
}
}
function testwatch() {
let _owner = user.id;
wixData.query("Watchlist")
.eq("title", movie_name)
.eq("movieitem", movieitem)
.eq("_owner", _owner)
.find()
.then((results) => {
let count = results.totalCount;
if (count === 0) {
$w("#addtowatchlist").show(),
$w("#added").hide(),
$w("#logintosave").hide();
} else {
$w("#added").show(),
$w("#addtowatchlist").hide();
}
});
}
function dothething() {
let _owner = user.id;
wixData.query("Watchlist")
.eq("title", movie_name)
.eq("movieitem", movieitem)
.eq("_owner", _owner)
.find()
.then((results) => {
let items = results.items;
let firstItem = items[0];
let count = results.totalCount;
if (count === 0) {
let toSave = {
"title": movie_name,
"movieitem": movieitem,
"_owner": _owner
};
wixData.save("Watchlist", toSave);
$w("#added").show(),
$w("#addtowatchlist").hide();
} else {
wixData.remove("Watchlist", firstItem._id);
$w("#addtowatchlist").show(),
$w("#added").hide();
}
});
}
export function addtowatchlist_click() {
dothething(movie_name);
}
export function added_click() {
dothething(movie_name),
removeFromWatchlist();
}