Sporadic issues with data hooks

I observed sometimes a data hook will not properly execute. On average the data hook will work.

How the one data hook works is, a item is inserted into a data set. It triggers an update to the same data set, then an email is sent via sendgrid.

When there is an issue, the data is not updated and a trigger is not sent. When I see this, I delete the row from the table and run the operation again and I can’t repeat the issue.

This seems to be a random occurrence.

Thoughts?

Here’s an example of a hook only working half the time.

In the following table (screen capture), the first row should be updated with the value of the _ID field.


Here is the code:

export function SomeDatabase_afterInsert(item, context) {
let toUpdate = {
“_id”: item._id,
“key”: item.key,
“some_other_key”: item._id
};
wixData.update(“SomeDatabase”, toUpdate);
}

Why isn’t it working all the time? Empty fields means it failed part way or didn’t even execute. However, as you can see, it works half the time.

I think the issue might be when updating a table with a NEW primary key when it was previously blank. Again, doesn’t happen all the time.

This doesn’t seem to be a hook problem. The update() function is just that - update. It can only be performed on an item that is identified by an _id . If there is no _id , then the update() function has nothing to update since it doesn’t know which item to update.

@yisrael-wix Is it a timing issue? Again, it doesn’t happen all the time. Would a delay ensure that the ID is there?

@brett82 Hmm, now that I look at it again, I’m not sure what the _id is and what is the key. Is the key the field that is sometimes blank?

Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor.

@yisrael-wix

I’m using the _ID value in the data hook (Channel_Invite_afterInsert) after insert to populate the Primary Key (invite_key) with a unique value, so I can reference it another table with a reference field.

I do this for a bunch of tables.

The Channel_Invite_afterInsert hook (as others were I use the same methodology) fails randomly.

Thanks!

@brett82 Am I supposed to be looking at the Invite page? How about login credentials that I can use to test? Anything else? Keep in mind that the only way I can help is if I have a way to properly test.

Thanks

@yisrael-wix

  • Use the “My Channels” page.

  • Navigate to a channel (Channel Pages (Dynamic)).

  • Invite an email to the Channel.

  • A new entry will be made on the table Channel_Invite.

  • The hook Channel_Invite_afterInsert will then be triggered.

  • The value from _ID will be used to populate the Primary Key (invite_key) with an update statement.

  • An email will then be sent via sendgrid.

It works most of the time. Sometimes it sporadically fails on the update.

Thanks again!!!

@yisrael-wix

Fair enough. Let me think about this. I’ve built something fairly unique. I’m using the invite as a way of validating user emails. For the scenario we are testing, it’s hard to simulate in the test environment. BTW: Is it possible reference fields might be creating issues with update statements, since the references have to be joined with a primary key in another table?

@brett82 I’m not sure what’s going on. Is there a case where the record doesn’t yet exist? In that case update() won’t work, but save() would.

Take a look at update() and you’ll see that it only updates - that is, if a record with the _id already exists it will be updated. If it doesn’t exist, then nothing happens. As the description for update() states: Updates an item in a collection.

On the other hand, save() might be better suited to your purpose. As the description states: The save function inserts or updates the specified item, depending on whether it already exists in the collection.

Perhaps save() would solve your problem?

@yisrael-wix

I’ll try to do some more testing tomorrow. However, it seems like the statement is having issues because the insert is not completely finished before the update commences. I can’t prove it yet, but that’s my hypothesis. It might be because I have reference fields in the databases.

So far, I haven’t any issues when I’ve added a setTimeout to some of the update statements.

setTimeout(() => {
wixData.update(“Channel_Events”, toUpdate)
}, 1000);

Thoughts?

Does that make sense?

Hey @brett82 ! So, I was muckin’ around your code some and discovered this:

In the backend data.js file, you have a function Channel_Events_afterInsert(). The database collection field key is incorrect.

You have:


And the field key should be:


That is: broadcast_event_key and NOT Broadcast_event_key . You need to be very careful to use the exact Field Key as displayed in the Manage Field panel.

Hopefully that’s it.

Let me know as soon as you find out - the suspense is killing me.

Yisrael