can i display the number of items in a collection to a text field on my page?

can i display the number of items in a collection to a text field on my page? if so can someone please help me, it would be much appreciated.

my collection is called barbersdatabase and the text to display the number of items is called #text52

thank you to anyone who can help

import wixData from ‘wix-data’;
$w.onReady( async function () {
const results = await findNumber()
$w(“#text52”).text = results.toString()
});

function findNumber() {
return wixData.query(“barbersdatabase”)
.find()
.then((res) => {
return (res.totalCount);
})
}

You can also use the query count() function.

Thank you so much. i am so great full for your help. it works exactly how i wanted. thank you again

thank you so much for your help. the answer above worked perfect, But what you sent me helped me solve another problem. thank you again

1 Like

@jakegriffin1990 pay attention - the first answer will work only if you don’t have more than 50 records in your collection (if you use limit(1000) than it’ll be up to 1000).
Also Yisrael’s answer will probably work a little bit faster as you’re sending less info to the front-end.

@jonatandor35 thank you for your response. do i change the 50 limit on the dataset or do i do this by adding code. thank you

@jakegriffin1990 as I said you should go for the count() query:

import wixData from 'wix-data';
$w.onReady(function () { 
    wixData.query("barbersdatabase").count()
    .then(total => {
    $w("#text52").text = total.toString();
    })
})
1 Like

@jonatandor35 thank you so much for taking the time to offer me advice. your code works perfect on my website. thank you again

1 Like