Search.../

afterQuery( )

A hook that is triggered after a find operation, for each of the items in the query results.

Description

The afterQuery() hook runs when:

  • The find function is called.
  • An action is performed on a dataset that retrieves items from the collection.
  • The collection is viewed in the CMS.
  • An item is exported from the collection.

The hook runs once for each item in the collection.

Return an object or a Promise that resolves to an object from the afterQuery() function. The returned object will be used as the result of the call to the find function instead of the actual item found in the collection. If returning a Promise, the object 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.

Note:
We recommend implementing afterQuery() with in-memory operations only. Don't use network calls such as REST API requests or other wix-data operations, as these can lead to timeouts and issues loading collection data.

Syntax

function afterQuery(item: Object, context: HookContext): Promise<Object> | Object

afterQuery Parameters

NAME
TYPE
DESCRIPTION
item
Object

One of the items of the query result. The hook is called for each item in the results.

context
HookContext

Contextual information about the hook.

Returns

The item to return to find instead of the item retrieved from the database. Returning a rejected promise will not block the operation, but will return a rejected promise to the operation caller as well as trigger the onFailure() hook.

Return Type:

Promise<Object>

 | 

Object

Was this helpful?

An afterQuery hook

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

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