beforeUpdate( )
A hook that is triggered before an update()
operation.
Description
The beforeUpdate()
hook runs when:
- The
update()
function is called. - An action is performed on a dataset that updates an item from the collection.
- An item is updated using the Content Manager.
The hook also runs when an action is performed on a dataset that updates an item from the collection that the dataset is connected to.
Return an object or a Promise that resolves to an object from the beforeUpdate()
function. The returned object will be updated in the collection instead of
the original item passed to the update()
function.
If the returned value is of the wrong type, the value is ignored.
A rejected Promise blocks the call to update()
and also calls the onFailure()
hook if it has been registered.
Because the beforeUpdate()
hook is called before the update()
is executed, it can affect the item that is updated in the collection
or block the update()
.
Syntax
function beforeUpdate(item: Object, context: UpdateHookContext): Promise<Object> | Object
beforeUpdate Parameters
NAME
TYPE
DESCRIPTION
The original item to be updated.
Contextual information about the hook.
Returns
The item to be updated instead of the original item 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:
|
ObjectWas this helpful?
1// In data.js23export function myCollection_beforeUpdate(item, context) {4 let hookContext = context; // see below56 // some change to the received item78 return item;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_beforeUpdate(item, context) {4 let hookContext = context; // see below56 // some change to the received item7 item.title = toUpperFirst(item.title);8 item.first_name = toUpperFirst(item.first_name);9 item.last_name = toUpperFirst(item.last_name);1011 return item;12}1314function toUpperFirst(s) {15 return s.charAt(0).toUpperCase() + s.slice(1);16}1718/*19 * hookContext:20 *21 * {22 * "collectionName": "myCollection",23 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",24 * "userRole": "siteOwner"25 * }26 */