How can I display the results of DataQuery count ( )

I would like to count specific items in my database and display the results. I used the coding below. I did not manage to display the number on text22. Any recommendations?

import wixData from ‘wix-data’;

// …

wixData.query(“Vacancies”)
.eq(“title”, “Accounting”)
.count()
.then( (num) => {
let numberOfItems = num;
$w(“#text22”).text = num;
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
} );

Do you have any messages (errors) in the Developers console?
Are you sure that the collection has entries that match the query?

I tried my own experiment just to be sure. I received 7 as the count in my collection. I set the text field like this:

$w("#text22").text = num;

Howerver, the variable num is a Javascript number which results in this error:


To fix the error, modify your code to force the variable to be converted to a string:

$w("#text22").text = "" + num;

I hope this helps,

Yisrael

1 Like

Yisrael I would like to thank you, it works for me now after your correction. Thanks once again.

1 Like

Hi thanks for posting this. So I used your code with mine to count and display followers, however, I can’t seem to get it to work. I don’t have any errors, it just isn’t counting anything. I changed this code:

$w("#text12, #text12").text = "Followers " + num;

To this to see if it wasn’t displaying right:

$w("#text12, #text12").text = "Followers " + 1;

But that works fine. I think it might have something to do with this part of the code:

wixData.query("Followers") // < I know what this does, lol.
    .eq("Title", "Count") // < I'm not sure about this part.

I don’t know really what this part does. ^

Would anyone be willing to help? I’d greatly appreciate it! This is my whole code:

import wixUsers from "wix-users"; 
import wixData from "wix-data";

export function button5_click(event, $w) { 

wixData.query("Followers")
   .eq("Title", "Count")
   .count()
   .then( (num) => {
 let numberOfItems = num;
    $w("#text12, #text12").text = "Followers " + num;
  } )

 const followersId = wixUsers.currentUser.id;

    console.log("followersId: ", followersId);

 const followeesId = $w("#dataset5").getCurrentItem()._owner;

    console.log("followeesId: ", followeesId);
 

    wixData.insert("Followers", { 

 "followees": followeesId, 

 "followers": followersId     

    }) .catch((err) => { 

        console.log(err);

   }); 
 }

@alex58407 In the filter conditions in your query, make sure you use the Field Key and not the Field name:


Instead of this:
.eq(“Title”, “Count”)
You want this:
.eq(“title”, “Count”)

1 Like

@yisrael-wix Thank you for replying! So I changed “Title” to “title”, but it’s still not counting. When I test the button it displays as 0. Would it have something to do with “Count”? Should I use the field key for that too, or is that for something else?

@alex58407 Since your collection is only for followers, you don’t need a filter, all you need is count().

This will give you the number of items in the collection:

wixData.query("Followers")
   .count()
   .then( (num) => {
      let numberOfItems = num;
      $w("#text12").text = "Followers " + num;
  } )
1 Like

@yisrael-wix Oh right! Thank you so much, it’s working now!

1 Like

Hi Yisrael, your code helped me a bunch! Thank you so much!

But I’ve got another question, what if I want to display a different message if the count is 0? How do I do that?

This is my code currently.

wixData . query ( “Recall” )

. eq ( “title” , $w ( “#vininput” ). value )
. find ()
. then ( res => {
$w ( “#recalltable” ). rows = res . items
$w ( “#recalltable” ). expand ()

});
wixData . query ( “Recall” )

. eq ( “title” , $w ( “#vininput” ). value )
. count ()
. then ( ( num ) => {
let numberOfItems = num ;
$w ( “#textresult” ). text = “You have " + num + " recall ongoing. Please contact Suzuki Malaysia Sdn. Bhd. Glenmarie Shah Alam Office at 03-5566 5688 for any enquiries. Thank you.” ;

$w ( ‘#textresult’ ). show ();

})
. catch ( ( error ) => {
let errorMsg = error . message ;
let code = error . code ;

});
}