basic count of items in the collection

I have a problem, maybe I am doing something wrong because I am extremely beginning programmer, but I took the code snippet from Wix API and it doesn’t want to work for me.
Here is the code:

import wixData from "wix-data";
$w.onReady(function () {
});
export function offersnumber_viewportEnter() {
wixData.query("Oferty") //my collection name
.count()
.then( (num) => {
let numberOfItems = num;
$w("#offersnumber").text = "" + num; //text field ID placed in footer
} )
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
} );
}

Is there a problem currently with counting the items from collections or I am really so dumb ?
Also can you suggest how to get a number of blog posts, forum posts, registered users, subscribers ?
I want to make a statistics box for my website.
Maybe it is a problem to place it in the footer ? Of course the code above goes into site tab.
Please help its important to me.
Thank you in advance :slight_smile:

Hi roomack -

Your code should work as is. However, you may need to resize your web browser window first and then scroll to your footer. The “viewport” is the area of your web browser that is visible. So technically when you scroll down to the footer, then your #offersnumber “viewportEnter” event should fire.

Another approach that may have more consistent results is:

CODE:
--------

$w.onReady( function () {
//TODO: write your page related code here…
getCounts();
});

function getCounts() {
wixData.query(“Oferty”) //my collection name
.count()
.then( (num) => {
let numberOfItems = num;
$w(“#offersnumber”).text = “” + num; //text field ID placed in footer
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
} );
}

Hope this helps.

Will look into getting the other information you asked about (i.e. # of blog posts, forum posts, etc.)

Best,
Nick

Thank you very much, Nick :slight_smile:
I know what happened - I was seeing it without scrolling. If the field didn’t enter the viewport, but has been there already, the code could not execute because it wasn’t triggered. Your code seem better, but doesn’t work. Should I place some trigger into the text field ?
Looking forward for the other count possibilities.

Kind regards,
Derek

Glad to help Derek.

As far as getting the counts for # of Blog posts, # of Forum posts, # of Registered Users, & # of Subscribers, it appears that none of the Wix Code API’s allow access to this information (as of yet).

Neither the Wix Blog nor Wix Forums has a Wix Code API. Which would be the logical place to find a property to get the count of posts for either one of these.

Also, unfortunately, neither the Wix Users or Wix CRM API provide for a “count” property to get the count of Members/Users or Subscribers.

Maybe someone from Wix will be able to provide more insight.

Best,
Nick

Thank you very much Nick for your time and checking on this for me.
Anyway, Wix is great. I am not a programmer - tried to learn php but failed - so went visually to try build my dream website. Wix allows more than joomla now and any standalone competitors. I am excited to develop my website alongside of Wix. :slight_smile: