Hooks Tutorials

Are there any video or interactive tutorials on using data hooks? I’m just not understanding how hooks work, which is a huge pain. I’ve ready just about everything Wix has, but I need to see someone use hooks to get how to use them.

Hi,
Currently, I’m afraid that we don’t have video tutorials for it. Can you please clarify what are your difficulties and what you were trying to achieve?

Thanks,
Tal.

Hey, Tal! Thanks for answering!

I don’t understand how to construct them, what they should look like (in the backend) and how they connect to databases and frontend code. I’ve read the articles and looked at hooks code, but I just can’t wrap my mind around how they work into the bigger page code picture and the parts of a page (like buttons, databases, current frontend coded interactions, etc.)

Caty

Hey there!
Hooks run in the background so you do not use them in the front end at all. If you for example add a data hook AFTER INSERT on a Data Collection called SIGNUPS. When someone inserts a records into the Data Collection SIGNUPS the code within that data hook will be executed in the background.

It is useful if you want to do stuff when things changes in the data collections, alter or modify the data itself, send an email for all new records or maybe insert a new record in some other data collection with parts of this data.

So only background work in data hooks.

But so powerful!

Andreas! OK! So that’s the most helpful way ANYONE has ever phrased data hooks to me!

So, here’s one thing I’m struggling with…

I’m working on a web based application for clients. I want them to be able to log emotional changes (which go into a database collection.) This is already set up and working. The second portion, though, is linking the first collection to multiple other collections, where the data can be orginized in different ways. How do I perform that link, with a hook? I want to take the raw data, from collection one, and disperse it to be utilized in different ways, through the use of other database collections.

Hi,
You can use hooks to save the data differently in different databases.
Let us know if you need help with the code.

Good luck :slight_smile:

So I am using export function afterSave to submit data entered in a form to an api and then displaying the response on the screen. I am having trouble saving the response in the database as it is already saved. Would a hook work in this case?

No, hooks is only for background work/tasks. A hook can’t interact with the users screen is any way possible. If you have saved data from a form let’s say in a Data Collection named Tasks the code inside the hook afterSave will be executed and you can send data to external api but you can’t interact with the users screen from a hook.

If you want to show results to users you must code that in the pages code not in backend folder. You can send stuff to functions in backend (not hooks) and get data in return that you show the user. But every interaction with page elements must be done in pages code.

Does that include search boxes? You can not query a field that has a hook? What is purpose of a hook then?

https://www.wix.com/code/home/forum/community-discussion/code-for-beforeinsert-hook-combining-data-from-multiple-fields-into-one Thoughts?

@jeff-haskins Hooks is to be seen as background logic. Let’s say you have a hook on AfterSave on a Data Collection. So when that record is saved you need to update some other system through an external API you can do that in the hook not on the page. This makes cleaner code in your pages if you move your data logic that do not affect the UI for the user to hooks instead on the page.

Also it makes the pages faster if you have that in hooks instead of code in pages.

So any changes that you always want to make after something has been modified, inserted or deleted in your data collections you can use hooks.

It is extremely powerful, hooks and routers are very very powerful in Wix Code.

@andreas-kviby if its extremely powerful then can i edit one database in the hook of another database? So when i add to a generic database a page is saving to it should hook to then edit my product pages, but it seems the only way you can ever change the inventory number of a product is by manually changing it in the dashboard as the admin account or the item actually being purchased. There are other situations you’d want to change the inventory and it seems i can only “get” from the “products” database not “set” from it, ie. theres: " getProductOptionsAvailability( )" but not “setProductOptionsAvailability( )”, that should be possible!!

@franklinshaw hey, that is because Products is read only for now so it has nothing to do with functions in WIX Code it is a permission issue so until WIX lets you write to that collection it wont be possible. Create a addin data collection and keep your stock there?

@andreas-kviby seems thats what im doing for now, a requests database people add to for manual admin approval who will make database changes, just trying to automate as much as possible

ah whole new products database would be unsafe, and impossible to migrate to now

@franklinshaw Not a whole one, just a tiny one to hold your stuff you need to update

Hi Guys,

Does the hook trigger if I make any adjustment in the database manually? That would be useful for me.

Regards