Why Does My Dynamic Capture Record Count from Members/PrivateMembersData collection post correctly to console log but get error on website

When I run the code below, the correct output posts to the console log, but I get the following error that posts to my website’s Home page: [object Promise]

The code outputs the following: Registered Connect Members: [object Promise]

Can someone help me understand why I’m getting this error?

Backend Function:
import wixData from ‘wix-data’ ;

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

export function getMembersNum () {
wixData . query ( members_col ). find ( options ). then (( x ) => {
let allResults = x . totalCount ;
console . log ( allResults ); //this is the number of results

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

Page Code:
import { getMembersNum } from “backend/getMemberCount.jsw”

$w . onReady ( function () {
let allResults = getMembersNum ();
$w ( “#text164” ). text = "Registered Connect Members: " + allResults ;

});

In getMembersNum(), you need to ensure that the Promise is returned by adding a return to the “beginning of the chain”, that is, before wixData.query(). Something like this:

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
      return allResults;
   }).catch(err => {
      return Promise.reject(err);
   })
}

See Returning in a .then()

Then in the frontend code, you need to handle the Promise returned from getMembersNum(), like this:

$w.onReady(function () {
   getMembersNum().then(function (allResults) {
      $w("#text164").text = "Registered Connect Members:   " + allResults;
   });
});

See Velo Web Modules: Calling Backend Code from the Frontend.

See the following for more information about Promises:

  • Promises, Promises

  • Velo: Working with Promises

BTW - a simpler way to get the record count is by using WixDataQuery.count() .

Thank you Yisreal!

Previously, I was running into the total count only being correct when running program as admin in preview but not when site was published as one of the users - count was always ‘1.’. I’ll also try it using WixDataQuery.count() per your suggestion. Thank you!

Yisrael, that fix with my existing code that you provided worked! Thanks so much.

However, my count statement is right above my website menu which appears on every page. And the count only appears when I’m on the Home page. When I navigate to other pages the number is removed with no number appearing. How do I get the count to appear for the entire website session no matter what page the user is on?

Is there a way to achieve this by not having to put the page code on every page, put for once for the entire site?

WAIT…I think I found my own answer…I can put the Page code in masterPage.js (Global Site) to load automatically on every page of the site.

@northamericatoafrica You can use the wix-storage API . Save the count using setItem() on the Home page. On the other pages you can use getItem() .

But why doesn’t the number stay the same on the other pages? Are you clearing it somewhere? Do you have any code in masterPage.js?

@yisrael-wix Not clearing it anywhere…that I can tell. I had the page code in Home page. Count only appears when I am on Home page. Was gonna try now putting in masterPage.js, which sounds like it will solve that problem…

@yisrael-wix Looks like that solved the problem…putting page code in masterPage.js. Thank you Yisrael very much!