beforeRemove( )
A hook that is called before a remove()
operation.
Description
The beforeRemove()
hook runs when:
- The
remove()
function is called. - An action is performed on a dataset that removes an item from the collection.
- An item is deleted using the CMS.
The hook also runs when an action is performed on a dataset that removes an item from the collection that the dataset is connected to.
Return a string or a Promise that resolves to a string from the beforeRemove()
function. The returned string will be used as the itemId
parameter for the
remove()
operation. The item with the new itemId
will be removed 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 remove()
and also calls the onFailure()
hook if it has been registered.
Because the beforeRemove()
hook is called before remove()
is executed, it can affect the item that is removed from the collection
or block the remove()
.
Syntax
function beforeRemove(itemId: string, context: UpdateHookContext): Promise<string> | string
beforeRemove Parameters
NAME
TYPE
DESCRIPTION
The ID of the original item to be removed.
Contextual information about the hook.
Returns
The ID to be used for the remove()
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_beforeRemove(itemId, context) {4 let hookContext = context; // see below56 // change to the item to remove78 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_beforeRemove(itemId, context) {4 let hookContext = context; // see below56 // change to the item to remove7 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 */