Jilu
Jilu

Reputation: 67

I'm unable to get the total count of records in a collection - Wix Code

I'm trying to get the total number of records in a wix collection using the code show below:

wixData.query("Client").count().then((num) => {
              clientTableIndex = num;
          }).catch((error) => {
              let errorMsg = error.message;
              let code = error.code;
              console.log("@Public IndexOf().");
              console.log(code + ": " + errorMsg);
          });

But for some reason its not giving me the count, neither it is showing up any error. I also tried using the find() like so:

wixData.query("Client")
                .find()
                .then((num) => {
                    clientTableIndex = num.totalCount;
                }).catch((error) => {
                    let errorMsg = error.message;
                    let code = error.code;
                    console.log(code + ": " + errorMsg);
                });
            console.log(clientTableIndex);
            let insertToClient = {
                "memberId": wixUsers.currentUser.id,
                "title": "Client0".concat(clientTableIndex + 1)

But still no luck. I've given full permission to the Collection Client in wix database. See below:

enter image description here

I'm kind of stuck at this moment. I'm sure, something I'm missing, which causing this issue.

Any help will be greatly appreciated.

Upvotes: 0

Views: 404

Answers (1)

Shan
Shan

Reputation: 958

The 1st method is correct. You are setting the number to a variable which may be the reason you are not "seeing" the number.

Set it to a text value or console log it like below to see the count

wixData.query('Client').count().then( (num) => {
    console.log(num);
    $w("#text1").text = "" + num;   
});

Upvotes: 1

Related Questions