Dynamically Capturing Record Count from Members/PrivateMembersData collection to post on Home page

I’m trying to dynamically capture the count of records from my Members/PrivateMembersData collection table and post on my home page. I have the following page code (see below). But when I execute the code in the editor by previewing the page, I get the correct count in the console log and posted on the home page. However, when I run the published version of my website as one of the registered site members, the count on the Home page always posts as ‘1’ and not correctly as ‘4,’ since there are 4 records in the collection table.

  1. Can someone help me understand this difference in behavior?

  2. Can you help me with a fix so that the correct count appears by any website user on the Home page when the site is published?

  3. I only have code writing in the page…do I need any backend js code?

Thank you in advance.

MY PAGE CODE:

import wixData from ‘wix-data’ ;

const members_col = ‘Members/PrivateMembersData’ ;
const options = { suppressAuth : true };

export function getMembersNUM () {
return wixData . query ( members_col ). find ( options ). then (( x ) => {
let allResults = x . totalCount ;
console . log ( allResults ); //this is the number of results
$w ( “#text164” ). text = "Registered Connect Members: " + allResults ;
return allResults ;

}). catch ( err => {
return Promise . reject ( err );
})
}

$w . onReady ( function () {
getMembersNUM ();
});

So the reason it’s not working is because the Member/PrivateCollections is private. Meaning only admin can read it. That’s the case in preview but not on your live website.

You need to create a backend function getMemberCount which execute the query with the option suppressAuth: true. That way you’ll execute the query as an admin in the backend

Thank you Kentin!

How do I integrate that with the Page code that I showed you? With adding the backend code, do I have to change any of the page code that I already have? I would think I’d have to call the backend code, some how, from the Page code, right? I’m very new to WIX Velo coding, but do have previous coding experience - structured programming in C , sql, little java in Unix platform - from years ago.

I see now, I have to import into frontend the backend code like such…

import {functoin_name) from 'backend/function_name.jsw';