I have a collection called People, and I’m trying to create a hook that will copy the ID field into the Title field when a new entry is added. (That way I’ll have a field other than ID that I know always exists and is unique, to use as a reference field in other tables, since you’re not allowed to use the ID field.)
For the time being, I’m trying to write the hook on a test page which includes only the bare minimum I need. So I’m entering data on the page https://dwbolt4.wixsite.com/sfcknox/blank-7 , which just has a field for the user’s last name and a submit button. All the work is done by basic Wix elements - a dataset with the field and submit button connected to data. There is no code to debug on this page.
I created a very simple hook to run before insert, copying the _id field to the title field. Here’s the original hook:
export function People_beforeInsert(item, context) {
item.title = item._id;
}
This worked fine in preview mode. I was able to add a new person to the table, and their ID was copied into the title field. But when I tried it on the main site, the person was added with no trouble (so I’m certain the problem is with the hook, not submission in general), but the title field remained empty.
I’ve looked in the forum for advice about this. I’ve seen people suggest that you have to return a Promise that resolves to the new value of the item. I’ll admit, I can’t see why that would be an issue on the main site and not in preview mode, but I tried it anyway. The current state of my hook is this:
export function People_beforeInsert(item, context) {
item.title = item._id;
return Promise.resolve(item);
}
Unfortunately, that didn’t help. The hook still works in preview mode and not on the main site.
I’m wondering if I’ve just missed something about how to get a hook which was written in the sandbox to run on the main site. I have not been able to find any specific information about how this is done. I can’t find any option to create or view hooks on the main site either - only in sandbox mode - so I feel like I’m just having to take it on faith that the hook I wrote is actually there. I have both published my site and synced my database since creating the hook - is there something else I need to do to make it run on the main site? Or if that’s not the problem, does anyone have any ideas about what else might be wrong?