Search.../

beforeGet( )

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

Description

The beforeGet() hook runs when the get() function is called.

The hook does not run when the find function is called or when a dataset retrieves items from the collection it is connected to.

Return a string or a Promise that resolves to a string from the beforeGet() function. The returned string will be used as the itemId parameter for the get() operation. The item with the new itemId will be retrieved instead of the item with the original itemId.

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

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

Because the beforeGet() hook is called before get() is executed, it can affect which item is retrieved or block the get().

Syntax

function beforeGet(itemId: string, context: HookContext): Promise<string> | string

beforeGet Parameters

NAME
TYPE
DESCRIPTION
itemId
string

The ID of the original item to be retrieved.

context
HookContext

Contextual information about the hook.

Returns

The ID to be used for the get() operation instead of the original itemId specified by the caller. 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:

Promise<string>

 | 

string

Was this helpful?

A beforeGet hook

Copy Code
1// In data.js
2
3export function myCollection_beforeGet(itemId, context) {
4 let hookContext = context; // see below
5
6 // change the item to get
7
8 return itemId;
9}
10
11/*
12 * hookContext:
13 *
14 * {
15 * "collectionName": "myCollection",
16 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
17 * "userRole": "siteOwner"
18 * }
19 */
Change which item is retrieved using a beforeGet hook

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