Search.../

beforeCount( )

A hook that is triggered before a count() operation.

Description

The beforeCount() hook runs when:

  • The count() function is called.
  • The collection is viewed in the CMS.

Return a query or a Promise that resolves to a query from the beforeCount() function. The returned query will be used as the query for the count() operation.

Often, you will modify the query that is received in the query parameter by calling one or more WixDataQuery functions.

If the returned value is of the wrong type, the value is ignored.

A rejected Promise blocks the call to count() and also calls the onFailure() hook if it has been registered.

Because the beforeCount() hook is called before count() is executed, it can affect how items are counted or block the count().

Syntax

function beforeCount(query: WixDataQuery, context: HookContext): Promise<WixDataQuery> | WixDataQuery

beforeCount Parameters

NAME
TYPE
DESCRIPTION
query

The original query as defined by count().

context
HookContext

Contextual information about the hook.

Returns

The query to be used for the count() operation instead of the original query. Returning a rejected promise will block the operation and will return a rejected promise to the caller as well as trigger the onFailure() hook.

Return Type:

Was this helpful?

A beforeCount hook

Copy Code
1// In data.js
2
3export function myCollection_beforeCount(query, context) {
4 let hookContext = context; // see below
5
6 // some change to the received query
7
8 return query;
9}
10
11/*
12 * hookContext:
13 *
14 * {
15 * "collectionName": "myCollection",
16 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
17 * "userRole": "siteOwner"
18 * }
19 */
Change the query used for the count, using a beforeCount hook

Copy Code
1// In data.js
2
3export function myCollection_beforeCount(query, context) {
4 let hookContext = context; // see below
5
6 // some change to the received query
7 let newQuery = query.eq("status", "active");
8
9 return newQuery;
10}
11
12/*
13 * hookContext:
14 *
15 * {
16 * "collectionName": "myCollection",
17 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
18 * "userRole": "siteOwner"
19 * }
20 */