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
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?
1// In data.js23export function myCollection_beforeCount(query, context) {4 let hookContext = context; // see below56 // some change to the received query78 return query;9}1011/*12 * hookContext:13 *14 * {15 * "collectionName": "myCollection",16 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",17 * "userRole": "siteOwner"18 * }19 */
1// In data.js23export function myCollection_beforeCount(query, context) {4 let hookContext = context; // see below56 // some change to the received query7 let newQuery = query.eq("status", "active");89 return newQuery;10}1112/*13 * hookContext:14 *15 * {16 * "collectionName": "myCollection",17 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",18 * "userRole": "siteOwner"19 * }20 */