Help with Error Message - UserError: datasetApi (...)

Hi, I am currently working on coding multiple search functions into my site. The first is a Text Input search bar the second is a Multi Checkbox.

They are both on my Dynamic Page featuring recipes. The recipes are in a database and displayed in a repeater. I want my search functions (search bar and multi checkbox) to search within my database and display results in the repeater format.

The code I’ve implemented into my search bar is working fine but when I’m in the preview mode … this error comes up in the Developer Console:

UserError: datasetApi ‘onReady’ operation failed Caused by DatasetError: Operation (onReady) not allowed during save

It indicates the error, I believe, is from Line 18 but when I’m in Editor mode, there is no error showing within the code. Wondering what I’m missing or needs to be fixed so this message doesn’t appear and everything runs smoothly.

Please see code below: (Line 18 indicated with ***)

import wixData from ‘wix-data’ ;

$w . onReady ( function () {

//SEARCH BUTTON TRIGGER 
$w ( "#searchButton" ). onClick ( **function**  () { 
    search (); 
}); 

//ENTER KEY TRIGGER 
$w ( "#searchbar" ). onKeyPress ( **function**  () { 
        search (); 
}); 

//SEARCH FUNCTION 
**function**  search () { 

    *** $w ( "#dynamicDataset" ). onReady ( **function**  () { 
        $w ( "#dynamicDataset" ). setFilter ( wixData . filter (). contains ( 'recipeCategory' ,  String ( $w ( '#searchbar' ). value )) 
                . or ( wixData . filter (). contains ( "title" ,  String ( $w ( '#searchbar' ). value ))) 
                . or ( wixData . filter (). contains ( "recipeTags" ,  String ( $w ( '#searchbar' ). value )))) 

            . then ( count ) 

        $w ( "#clearFilter" ). show (); 
    }) 
}  

//COUNT FUNCTION 
**function**  count () { 
    **let**  count  =  $w ( "#dynamicDataset" ). getTotalCount (); 
    **if**  ( count  >  0 ) { 
        $w ( "#results" ). text  =  ` ${ count }  recipes found` ; 

    }  **else**  {  $w ( "#results" ). text  =  `No recipes found` ; } 

    **return**  count ; 
} 

//SCROLL TO THE TOP WHEN PAGINATION IS CHANGED 
$w ( "#pagination1" ). onClick (() => { 
    $w ( "#anchor1" ). scrollTo (); 
}); 

//CLEAR FILTER  
$w ( "#clearFilter" ). onClick ( **function**  () { 

    $w ( "#searchbar" ). value  =  **undefined** ; 
    $w ( '#recipecheckbox' ). value  =  **undefined** ; 

    $w ( "#clearFilter" ). hide (); 
    $w ( "#dynamicDataset" ). setFilter ( wixData . filter ()). then ( count ); 

}); 

//COUNT BEFORE PAGE LOADS 
$w ( "#dynamicDataset" ). onReady ( **function**  () { 
    count (); 
}); 

});

Thank you so much!

Hi @katiesrogers01 ,

Try taking the $w ( “#dynamicDataset” ). onReady out of the search() function. You don’t want to call this every time the function executes, plus the dataset will already be ready by the time you are able to call search(). So the function would look like this:

//SEARCH FUNCTION
function search() {
$w(“#dynamicDataset”).setFilter(wixData.filter()
.contains(‘recipeCategory’, String($w(‘#searchbar’).value))
.or(wixData.filter().contains(“title”, String($w(‘#searchbar’).value)))
.or(wixData.filter().contains(“recipeTags”, String($w(‘#searchbar’).value))))

		.then(count) 

	$w("#clearFilter").show(); 
}  

Also, since you’re not doing anything with the response, you can leave out the .then() on setFilter().

Thanks @robmich
I input the new code you outlined but still get this populating when I preview and use the search bar

The search bar works as I want it too - filtering through recipes and displaying them in the correct format; just don’t understand the error and how it’s impacting my site overall. Any thoughts? Thanks!

Hi @katiesrogers01 , a couple of questions:

  1. Try to comment out the onKeyPress event handler and just use the button. Do you still get the error then? I’m wondering if executing the search function every time a key is pressed is causing an issue. Since SetFilter is a promise, then the code is trying to set a new filter before the previous call to setFilter has resolved.

  2. Is #searchbar connected to an element in the dataset? If so, then remove that link.

  3. Is there any code that is performing a save on the dataset? Maybe something in a data hook; like AfterQuery?