Question:
I’ve used the data.js file to attach hooks to multiple collections on a demo website I created. When I try to create the SAME data.js file in a live project it doesn’t work. Any ideas what I’m doing wrong?
Product:
Using the standard Wix editor
What are you trying to achieve:
I am trying to add a BeforeInsert hook to a collection, to make changes to the item before it is stored in the collection.
What have you already tried:
I’ve tried deleting and recreating the data.js file using the “add hooks” sidebar multiple times. When I remove the hooks the items are added to the collections perfectly; the minute I add ANY code in the data.js file it throws an error.
Additional information:
I tried making my hook extremely simple to isolate the hooks as the source of the problem. This is what my hook looks like:
export function ReceivingInventory_beforeInsert(item, context) {
return item
}
When I check the live site logs, it gives me these errors:
Data hook execution internal error.
[“backed/data.js does not exist”]
Error: Promise in backend function rejected with undefined.
The hook is 3 lines long, I can’t imagine there are any syntax errors in there.
The same hook works just fine in a demo website.
As I’ve mentioned I’ve tried removing the data.js file and adding it again multiple times.
Your functions runs but it throws error while running, error is not directly inside of your function. It’s more about what it should return."
First go and console.log the item inside the hook function. Check what is the data when you are running the hook.
If there is no problem with the data type and value then check docs of beforeInsert, if you are returning an object then your code should be good and running.
If you still see the error use onFailure hook to understand more about the error and reason.
Create another hook for the same collection for checking/understanding errors. You’ll probably fix the issue with these steps.
Hey! Thanks for the reply.
I’ve tried logging the item sent to the hook and the item returned from the hook multiple times. It is a simple JS object with no nesting.
I’ve also rewritten the backend function that calls the hook in the new WebMethod syntax, assuming that maybe the old .jsw that was calling the hook might be the culprit.
Now the error that it throws is different, and I am not sure how to debug or decipher the problem. Here is the new error’s JSON payload.
It also says “Data hook execution internal error”. As mentioned before, it is a very simple 3 line hook that returns the object.
"jsonPayload":{
"message":"["Both resolutions failed at _load!\n[]\nOriginal resolver: Cannot find module 'backend/data.js'\nRequire stack:\n- /user-code/stubmodule-that-does-the-require.js\n- /cloud-runtime-code/node_modules/scoped-require/index.js\n- /cloud-runtime-code/packages/elementory/cloud-runtime/factories.js\n- /cloud-runtime-code/packages/elementory/cloud-runtime/create-app.js\n- /cloud-runtime-code/packages/elementory/cloud-runtime/cloud-grid-runner.js\nYarnPNP: Both resolutions failed at _resolveFilename!\n[{\"moduleFunctionName\":\"_load\",\"codePath\":\"yarnPNP\",\"request\":\"backend/data.js\"},{\"moduleFunctionName\":\"_resolveFilename\",\"codePath\":\"yarnPNP\",\"request\":\"backend/data.js\"}]\nOriginal resolver: Cannot find module 'backend/data.js'\nRequire stack:\n- /user-code/stubmodule-that-does-the-require.js\n- /cloud-runtime-code/node_modules/scoped-require/index.js\n- /cloud-runtime-code/packages/elementory/cloud-runtime/factories.js\n- /cloud-runtime-code/packages/elementory/cloud-runtime/create-app.js\n- /cloud-runtime-code/packages/elementory/cloud-runtime/cloud-grid-runner.js\nYarnPNP: Your application tried to access backend, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.\n\nRequired package: backend (via \"backend/data.js\")\nRequired by: /user-code/\n\nRequire stack:\n- /user-code/stubmodule-that-does-the-require.js\n- /cloud-runtime-code/node_modules/scoped-require/index.js\n- /cloud-runtime-code/packages/elementory/cloud-runtime/factories.js\n- /cloud-runtime-code/packages/elementory/cloud-runtime/create-app.js\n- /cloud-runtime-code/packages/elementory/cloud-runtime/cloud-grid-runner.js"]"
}
"severity":"ERROR"
"receiveTimestamp":"2024-03-24T01:57:24.584Z"
Yes, I’ve tried multiple combinations of removing the file, reloading the page and rewriting the data.js file.
I have also tried adding different hooks to the collection; and adding hooks to other collections as well.
As soon as ANY hook is registered with a collection, any interaction with the collection fails.
If it provides any addition information, in addition to the previous “Both resolutions failed” error, I also get this “Data hook execution internal error”:
It’s a BUG not user mistake so it should be fixed by Wix there isn’t anything wrong. Same code with same setup works on one site and doesn’t work on another site which is interesting…
I’ve created a bug report with Wix, and sent them a video of the error happening in real time. Also sent them screen shots of all the errors and stack traces.
After talking to Wix support, and submitting screenshots and video evidence of the bug, they’ve fixed the Data Hooks on my website.
They said they can’t disclose the nature of the error or the fix, but said that it won’t happen in the future.
In the meanwhile, my workaround for the hooks was to publish the website whenever I made changes to the hooks. Simply making changes in the editor, or saving the website did not work.
In any case, they’ve fixed the issue now. Thanks Wix support.
P.S: When I opened a ticket with Wix support, I sent a bug report and not a regular support ticket.