Code Issues

Hi, I’m currently working with wix editor to biuld my website, and in 1 of my pages I have a documentary database where you have a search bar and a dropdown menu with all categories so the users can filter all the documentaries by category, for those two aspects I had to use code and it was fine, I watched a tutorial and everything worked fine.
But when I tried to to the exact same process for my page of the Library, where you can also search the Book by title or filter by categorie with a dropdown menu the code didn’t work.

The only change I made was the names of the elements and I added 5 more categories on the Library page, I checked the dataset the page defenitions, everything I can remember, and the code simply does not work.
Can someone help me plz?

Here is the code:

import wixData from “wix-data” ;

export function searchtitle_keyPress(event) {
let SearchValue = $w( “#searchTitleBook” ).value;
$w( “#bookDataset” ).setFilter(wixData.filter().contains( “title” , SearchValue));
}

export function categorydrop_change(event) {
let searchcat = $w( “#categoryDropBook” ).value;

$w( “#bookDataset” ).onReady( () => {

if (searchcat === “Spirituality” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “spiritualityB” , true ));
}
if (searchcat === “Veganism” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “veganismB” , true ));
}
if (searchcat === “Ambientalism” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “ambientalismB” , true ));
}
if (searchcat === “Relegions” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “relegionsB” , true ));
}
if (searchcat === “Philosophy” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “philosophyB” , true ));
}
if (searchcat === “Nature” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “natureB” , true ));
}
if (searchcat === “Music” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “musiBc” , true ));
}
if (searchcat === “Art” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “artB” , true ));
}
if (searchcat === “Drugs” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “drugsB” , true ));
}
if (searchcat === “Science” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “scienceB” , true ));
}
if (searchcat === “Old Civilizations” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “oldCivilizationsB” , true ));
}
if (searchcat === “Yoga” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “yogaB” , true ));
}
if (searchcat === “Meditation” ) {
$w( “#bookDataset” ).setFilter(wixData.filter().eq( “meditationB” , true ));
}

})
}

Your dataset might not be ready when the page is. You need to use the dataset’s onReady() event handler to ensure that the dataset is available before you attempt to use it.

Hi, and thank you for the fast response I just did what you told me and It didn’t work, and on the page where the code is working I also don’t have the dataset onReady and it works anyway.

@stefemengoncalves Please show the code.

Also, even though you don’t use the onReady() on the other page, doesn’t mean that you don’t need it. It’s to ensure availability of the dataset.

@yisrael-wix the code is the one on the this post

As Yisrael has already stated, you need to make sure that your page and dataset are ready before you run any code here.

Simply try moving your datasets onReady function in to the pages own onReady function, something like this.

import wixData from"wix-data";

$w.onReady( () => {
$w("#bookDataset").onReady( () => {
});
});

let SearchValue = $w("#searchTitleBook").value;
let searchcat = $w("#categoryDropBook").value;
export function searchtitle_keyPress(event) {
$w("#bookDataset").setFilter(wixData.filter().contains("title", SearchValue));
}

export function categorydrop_change(event) {
if (searchcat === "Spirituality") {
$w("#bookDataset").setFilter(wixData.filter().eq("spiritualityB", true));
}
// rest of code here//

Also, should Relegions be Religions and musiBc be musicB.

If you check out the Wix Corvid examples for search here.
https://www.wix.com/corvid/example/search-a-database
https://www.wix.com/corvid/example/checkbox-dropdown

You will see that both of them use the onReady function for the page so that it is ready before anything else happens on it.
https://www.wix.com/corvid/reference/$w.html#onReady
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onReady