Command priority not working

For some reason, the commands have the wrong priority. I have a table that I’m filtering and I want to find the number of items displayed. Although the counting command is after the filter, I get the number of items before filtering. How can I fix this?

export function dropdown1_change(event, $w) {
$w(“#dataset1”).setFilter(wixData.filter()
.contains(“criteria”, $w(“#dropdown1”).value));
let count = $w(“#dataset1”).getTotalCount();
console.log(count);
}

Hey Codrin,

After the setFilter, you need to wait till the promise is fulfilled. Your code should look like this:

export function dropdown1_change(event, $w) {
    $w("#dataset1").setFilter(wixData.filter()
    .contains("criteria", $w("#dropdown1").value))
    .then(() => {
        console.log("Dataset is now filtered");
        let count = $w("#dataset1").getTotalCount();
        console.log(count);
    })
    .catch((err) => {
        console.log(err);
    });
}

As you can see in the above snippet, the count is retrieved inside of the .then(), which is where the Promise is fulfilled.

For a gentle introduction to Promises, see the forum post Promises, Promises.
For a more comprehensive discussion of Promises, see the article Working with Promises in Wix Code.

Good luck and have fun,

Yisrael

1 Like

Hello Yisrael,

Thank you for your answer!
I have managed to make it work!

Regards!