Search.../

afterCount( )

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

Description

The afterCount() hook runs when:

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

Return a number or a Promise that resolves to number from the afterCount() function. The returned number will be used as the result of the call to count() instead of the actual count of items found in the collection. If returning a Promise, the number is used as the result, whether the Promise is fulfilled or rejected.

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

A rejected Promise also calls the onFailure() hook if it has been registered.

Syntax

function afterCount(count: number, context: HookContext): Promise<number> | number

afterCount Parameters

NAME
TYPE
DESCRIPTION
count
number

The number of items the count operation has found.

context
HookContext

Contextual information about the hook.

Returns

The count to return to count() instead of the original count. Returning a rejected promise will not block the operation, but will return a rejected promise to the caller as well as trigger the onFailure() hook.

Return Type:

Promise<number>

 | 

number

Was this helpful?

An afterCount hook

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

Copy Code
1// In data.js
2
3export function myCollection_afterCount(count, context) {
4 let hookContext = context; // see below
5
6 // return a value higher than the actual item count
7 count += 5;
8
9 return count;
10}
11
12/*
13 * hookContext:
14 *
15 * {
16 * "collectionName": "myCollection",
17 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
18 * "userRole": "siteOwner"
19 * }
20 */