None of my hooks seem to work

Here is some example code I have in data.js

Shouldn’t this update the column Event_Title (event_title)?

export function Events_beforeInsert(item, context) {
item.event_title = “Blah”;
return item;
}

Hello Brett.
Are you sure you are doing insert ,not update,for example, and your database name is Events(not events)?
If none of it helps, please provide me a link to your app.

The database name is Events, not events. The function was created by the database.

Here is where our development app is: https://brett979.wixsite.com/channelvillebeta

This guy is also having the same issues: https://www.wix.com/code/home/forum/questions-answers/hooks-not-being-called

I even tried to debug using the Chrome developer console from the database as per documentation. It looks like nothing gets called.

Your help is greatly appreciated.

Any ideas?

At one point I had a hook working and the one that was working seemed to stop working.

What could have changed? I can’t even debug hooks and I followed the instructions for debugging hooks.

It seems hooks are not even being called.

To make it easier to demonstrate the issues I’m having, I created a page that is not member protected: https://brett979.wixsite.com/channelvillebeta/test-hooks

It simply updates the first name (first_name) in the database. However, I have also created a new hook that should replace the first_name submitted value with ‘test’.

export function Profile_beforeUpdate(item, context) {
item.first_name = “test”;
return item;
}

But it doesn’t…

Others seem to have the same issue:

What am I doing wrong or is there an issue with the service?

Also, as a side note, how can I have duplicate entries for a primary key?

Coincidently, I created a hook, based on an example from the forums to prevent duplicate keys. That was the one was that has now stopped.

Hi Brett,

The problem is with the import of “wix-crm” in data.js.
Please remove that line and your hooks will work again (I tested your site, specifically the “test hooks” page).

I don’t know yet why it causes the data.js file not to load, I’ll update if I have more information.
Also, I think we should at least show an error in the console when that file fails to load. I’ll pass this feedback on.

Please let me know if it worked for you, waiting for your answer.

Hi again,
I learned some more on the new wix-crm capability, and it seems that the “wix-crm” package is for client side usage (for example in the page code).
There’s a “wix-crm-backend” that should be used in the backend (for example, data hooks):

Please try to use that instead and let me know if it works.

Thanks for the reply. I removed the wix-crm code. My goal at this point, is just to get hooks working.

As a start, it would be helpful to get the following functions in the data.js file working:

export function Profile_afterUpdate(item, context) {
item.first_last = “Blah”;
return item;
}

export function Profile_beforeUpdate(item, context) {
item.first_name = “Test”;
return item;
}

Also, why is Profile_beforeInsert not working?

The function is meant to prevent duplicate emails. It used to work, now it doesn’t. Furthermore, the email column is a primary key, shouldn’t that alone prevent duplicates?

beforeInsert hook worked for me, so again, I would need to see your complete site with the non-working hook to figure out what’s wrong.
The primary field doesn’t prevent duplicates, it’s purpose is more for referenced collections.
The ID field will still be the primary key of the collection.

I knew about the _id field. However, I wanted to confirm how the assigned primary field worked.

That said, you can test out the one hook that prevents duplicates at: https://brett979.wixsite.com/channelvillebeta/be-a-broadcaster

I thought it was working at one point.

You should not be logged in.

Here is the hook:

export function Profile_beforeInsert(item, context) {

let p = new Promise( function (resolve, reject)
{
wixData.query(“Profile”)
.eq(‘email’, item.email)
.limit(1)
.find()
.then(results => {
let resultCount = results.totalCount;
if (resultCount === 0) {
console.log(“count is zero, no dups found, resolving the promise to the original ‘item’ so the insert can proceed”);
resolve(item);
}
else
{
console.log(“count is more than zero, rejecting the promise with an error message”);
reject(“count is more than 0, dups found!”);
}
});
});
//return the promise from the hook
return p;
}

I also set up a form that tests another hook: https://brett979.wixsite.com/channelvillebeta/test-hooks

Again, you don’t need to be logged in.

I set up the following hooks to test:

export function Profile_afterUpdate(item, context) {

item.last_name = "Blah"; 
**return**  item; 

}

export function Profile_beforeUpdate(item, context) {

item.first_name = “Test”;
return item;

}

Shouldn’t the one hook update first_name with ‘Test’ and after the update, update the column last_name with “Blah” for the selected item?

I even modified the following example from Velo: Using Data Hooks | Help Center | Wix.com

export function collection_beforeInsert(item, context) {
item.title = item.title.toUpperCase();
return item;
}

to:

export function Events_beforeInsert(item, context) {
item.event_title = item.event_title.toUpperCase();
return item;
}

It didn’t work.

I don’t understand what is going wrong.

Thanks

Hi,
The problem is you’re importing “wix-users” instead of “wix-users-backend”.
Once I changed that, the “Test Hooks” page started working.
Please fix that, and then try all other hooks.

Thanks!!!

It worked!

Even console.log is now working, which is odd.

I spent 2 weeks wondering what I was doing wrong. However, in all fairness, the documentation is a little vague on pointing out this one really important nuisance. I’m wondering if this is the problem with others code.

It might be helpful to add a note on the following pages:

Thanks again.

At least you’re getting help now. It’s probably something simple. What other libraries are you importing?

I think you commented on the wrong thread :slight_smile:

Oops. Sorry. Tried to pay it forward. Thanks for the help. It made my weekend.