Count number of record in a collection

Please I need help counting the number of a record in a collection and displaying the number of time occurred, in a text fiel, here is my code, but not working

wixData.query(“#dataset1”)
.count()
.then( (num) => {
let numberOfItems = num;
$w(‘#txtRegcourse’).text = numberOfItems;
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
} );

something like this…

wixData.query(“formDatabase”) //change this to the name of your database
.find()

.then((results) => {
let allResults = results.totalCount;
console.log(allResults);//this is the number of results

$w(’ #txtRegcourse ').text = allResults;

Thanks, but its still not working, nothing happens when I trigger the event.

the console.log has to log some value, if nothing is happening then you have not executed the code correctly

I have tested the code and it 100% works

I am using a collection called ‘voters’. I need to count the number or voters and display it on my home page. As voters register the number needs to automatically update.

wixData.query(“Voters”)
.find()

.then((results) => {

let allResults = results.totalCount;

console.log(allResults);//this is the number of results

//$w(‘#input1’).number = allResults;
//$w(‘#input1’).type = allResults;
$w(“#input1”).text = “” + allResults;
$w(“#input1”).type = allResults.toString();
} );

No matter what I try the text field on the page (input1) will not display allREsults.

  1. Which field/s should I use for this?
  2. What code snippet should I use?
  3. Where must the code go, backend data.js file or in the code for the home page?

Thanx for your promp assistance. This is very urgent.

import wixData from ‘wix-data’;

export function page1_viewportEnter(item, context) {
item.Totalcount = item.totalMandates + 1 ;
return item;
}

wixData.query(“Voters”)
.find()

.then((results) => {
$w(“#dataset2”).getTotalCount();
let seq = $w(“#dataset2”).getTotalCount();
console.log(seq);

$w(‘#input1’).type = seq;

let allResults = results.totalCount;

console.log(allResults);//this is the number of results

//$w(‘#input1’).number = allResults;
//$w(‘#input1’).type = allResults;
//$w(“#input1”).type = allResults.toString();

$w(‘#progressBar1’).targetValue = 100;
// Set the progress as 20
$w(‘#progressBar1’).value = allResults;
} );
//============================================================

When I run this code - the console.log shows the number of voters. But I don’t know how to get that onto the home page in a field.

OMG - you are a genius!!
May I keep contact with you please Heath?
I am running a Voting campaign for my government - I don’t want to make a mistake.
Thanx

Thanx again