Search.../

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 CMS.

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
item
Object

The original item to be updated.

context
UpdateHookContext

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:

Promise<Object>

 | 

Object

Was this helpful?

A beforeUpdate hook

Copy Code
1// In data.js
2
3export function myCollection_beforeUpdate(item, context) {
4 let hookContext = context; // see below
5
6 // some change 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 to update using a beforeUpdate hook

Copy Code
1// In data.js
2
3export function myCollection_beforeUpdate(item, context) {
4 let hookContext = context; // see below
5
6 // some change to the received item
7 item.title = toUpperFirst(item.title);
8 item.first_name = toUpperFirst(item.first_name);
9 item.last_name = toUpperFirst(item.last_name);
10
11 return item;
12}
13
14function toUpperFirst(s) {
15 return s.charAt(0).toUpperCase() + s.slice(1);
16}
17
18/*
19 * hookContext:
20 *
21 * {
22 * "collectionName": "myCollection",
23 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
24 * "userRole": "siteOwner"
25 * }
26 */