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
The ID of the original item to be retrieved.
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:
|
stringWas this helpful?
1// In data.js23export function myCollection_beforeGet(itemId, context) {4 let hookContext = context; // see below56 // change the item to get78 return itemId;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_beforeGet(itemId, context) {4 let hookContext = context; // see below56 // change the item to get7 let newItemId = "1234";89 return newItemId;10}1112/*13 * hookContext:14 *15 * {16 * "collectionName": "myCollection",17 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",18 * "userRole": "siteOwner"19 * }20 */