setFilter don't work

hi everone
how fix it


import wixData from "wix-data"; 
$w.onReady(function () {
    const count = $w("#dataset3").getTotalCount();
    const Numb = Math.floor( (Math.random() * count) + 1);
    $w("#dataset3").setFilter( wixData.filter().contains('num', Numb) );
     
});

For starters you are wanting both your page and dataset to be fully loaded before you do anything on your page, which is clearly mentioned in the getTotalCount code example.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getTotalCount

Note
A dataset needs to load its data before you call its getTotalCount() function. Usually a dataset finishes loading a short time after the page it is on finishes loading. So if you call getTotalCount() inside the page’s onReady() event handler, the dataset might not be ready yet.

To call getTotalCount() as soon as possible after a page loads, use the dataset’s onReady() function inside the page’s onReady() event handler to ensure that both the page and the dataset have finished loading.

Get the number of items in the dataset that match its filter criteria when the page loads

$w.onReady( () => {
  $w("#myDataset").onReady( () => {
    let count = $w("#myDataset").getTotalCount();

  } );
  
} );
1 Like

thank you for reply

so what about filter
no work

let number1 = Math.floor( (Math.random() * count) + 1);
 $w("#dataset3").setFilter( wixData.filter().contains('num', number1) );
 

or

 $w("#dataset3").setFilter( wixData.filter().contains('num', 46) );
        

With regards to your filter, well you are using setFilter which is okay.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFilter

Examples

Set a dataset’s filter

This example filters a dataset to only have items where the lastname field starts with “D” and the age field is greater or equal to 21.

import wixData from 'wix-data';

// ...

$w("#myDataset").setFilter( wixData.filter()
  .startsWith("lastName", "D")
  .ge("age", "21")
);

However, you then use the contains filter function which only works with a String or a Reference, whereas you are trying to find a number value.
WixDataQuery - Velo API Reference - Wix.com

contains( )
Refines a query or filter to match items whose specified property value contains a specified string.

Description
The contains() function refines a WixDataQuery or WixDataFilter to only match items where the value of the specified property contains the specified string. Matching with contains() is not case sensitive, so “text” does contain “Tex”.

You can use contains() with a property whose value is a String or a Reference. For properties of type reference it is recommended that you use the eq() function instead of contains(). With properties that are References, contains() matches by the ID of the referenced item as a String.

Create a query, add a contains filter, and run it

import wixData from 'wix-data';

// ...

wixData.query("myCollection")
  .contains("description", "some words")
  .find()
  .then( (results) => {
// rest of your code...

“Matching with contains() is not case sensitive” - It is case sensitive for some fields (such as the “brand” field) of the Product collection of Wix Store. For example, “de” doesn’t match “De”.
Is there anyway to remove the case-sensitive constraint for all the fields of the Product collection?
Thanls