Data hooks run code before or after certain interactions with your site's collections. A data hook allows you to intercept the interaction immediately before or immediately after it occurs. The hook's code can even be used to affect the interaction itself. For example, you may want to intercept an item before it is added to your collection to perform a final validation or to tweak the data that actually makes it into the collection.
In general, hooks are run whether the interaction with your collection is initiated by a page element or programmatically using the
WixDataOptions object and use it to suppress hooks from being called on that particular interaction.. However, a Data API call from the backend code of your site may pass the optional
See to find out how to register hooks on your collections.
There are a number of different points in the lifecycle of a data interaction that you can hook into. Different hooks receive different arguments and return different values. For more information, see the .
item: The current item. For example, in
beforeInsert, this is the item that is about to be inserted. If there are many items, such as can happen with
afterQueryfor example, the hook function will be called repeatedly, once for each of the items.
itemId: The current item's ID value.
WixDataQueryobject that will be executed.
count: The number of items in the count.
error: An error object.