Count the number of posts in a category and push into an array.

The problem I am having is accessing the database and finding the correct category and then placing the number of posts in a new variable which will then be pushed into an empty array.

import wixData from 'wix-data';

let BooksHubCount 
wixData.query('Categories')
.eq('Books Hub','postCount')
.count()
.then((num)=>{
BooksHubCount = num

})

$w.onReady(function () {
    $w("#numberposts").text = String(BooksHubCount)
});

I am not sure what is wrong, but categories is a database inside the folder Forum, the ‘Books Hub’ is the name of the category and ‘postCount’ is the Id within the database.
Is there an easier way to complete this, if so please let me know.

Thank you.

Hello Jil,

i can not see your —> “Array-Push”, where is it?

I actually was trying to see if I could get the correct number in a text form that would display on the website before moving onto the next part of placing it in an array. Currently, the only output I am getting is “undefined”.

Here you can see an EXAMPLE how it works…

https://russian-dima.wixsite.com/meinewebsite/blank-27

import wixData from 'wix-data';

var myValue

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

function count_myItems() {
    myValue = $w('#DD1').value
    console.log(myValue)
    wixData.query("Corvid-Video-Tutorials")
    .eq('group',myValue)
    .count()
  .then( (num) => {
 let numberOfItems = num;
    console.log(numberOfItems)
    $w('#TXTresults').text=numberOfItems.toString()
  } )
  .catch( (error) => {
 let errorMsg = error.message;
 let code = error.code;
  } );
}

export function button1_click(event) {count_myItems()}
export function DD1_change(event) {myValue = $w('#DD1').value}

The table reflects the DATA of the connected DATABASE.
There are just 2-Goups in the DATABASE, which you can search for with the DropDown-Menu.

DD1 = DropDown-Menu
DB = “Corvid-Video-Tutorials”
REFERENCE = “group”

The link to your website is not working, however this helps. Thank you so much.

I will update in 3min
SORRY for the broken link.

https://russian-dima.wixsite.com/meinewebsite/count-items-in-database

Hi, thank you for your help.
I am actually having some trouble with returning the value and then pushing the value into an array. Is this how it would be done?

Currently, in the console, the output is undefined for all 5 values within the array. However, the value being printed when the function occurs for ‘console.log(numberOfItems)’ actually is a number.

import wixData from 'wix-data';

var statsPosts = [ ];


function count_myItems(myValue) {
 //myValue = "Books Hub"
    console.log(myValue)
    wixData.query("BlogPosts")
    .eq('type',myValue)
    .count()
  .then( (num) => {
 let numberOfItems = num;
    console.log(numberOfItems)
 return numberOfItems
  } )
  .catch( (error) => {
 let errorMsg = error.message;
 let code = error.code;
  } );
 
}

$w.onReady( () => {


    statsPosts.push(count_myItems("SuccessStory"));
    statsPosts.push(count_myItems("stuckathome"));
    statsPosts.push(count_myItems("LossStory"));
    statsPosts.push(count_myItems("Tested"));
    statsPosts.push(count_myItems("Other"));
    console.log(statsPosts)



});

Sorry in this case i think i can not help you. Never worked with return-command yet.

All i got is this …

import wixData from 'wix-data';

var myValue
var myArray = []

$w.onReady(function () { myArray.push(count_myItems("WIXSHOW")), console.log(myArray[0]) });


function count_myItems(searchValue) {   
 return wixData.query("Corvid-Video-Tutorials")
    .eq('group', searchValue)
    .count()
 
  .then( (num) => {
 let numberOfItems = num;
 return numberOfItems
  })
}

This surely can not be the right way.
Perhaps anybody else can help.

The process gets the right COUNT, but not in the right way.