I have a simple one page site with one button, and a dataset linked to a database; and a table linked to that dataset: peterjanderson6724.wixsite.com/website-1. Try it.
The page code is:
import wixData from 'wix-data';
$w.onReady(function () {
$w("#dataset1").onReady(() => {
$w('#table1').refresh()
});
});
export function writeTestData_click(event) {
let dateNow = new Date()
let toInsert = {
"testField": 'AAAA',
'comment': 'Test Insert',
'testDateText': dateNow.toString()
};
wixData.insert("TestData", toInsert)
.then((results2) => {
$w('#dataset1').refresh()
console.log('INSERT SUCCESS', results2);
})
.catch((err) => {
let errorMsg = err;
console.log('INSERT FAIL', errorMsg);
})
}
and there is a hook on the database ‘TestData’ as follows:
So what I expect to happen is that the page code inserts an item into the database with testField set to ‘AAAA’, and the hook changes the value of testField to 'BBBB.
This works fine in Preview, where all items in the database are written with testField value ‘BBBB’.
But on the live site all items in the database have testField value ‘AAAA’.
Why is the hook not firing on the live site?
Where am I going wrong?
It is entirely possible that in the midst of all these conversations, Wix has done some work behind the scenes and fixed the underlying problem. To Shan’s point above, handling promises is important, but making the data hook an async function does not change whether the Wix code calls it or not.
My solution to avoid future issues with data.js: i copied the various data.js functions into my own backend functions (.jsw) and then added page code to all them myself. I did not change the code in the functions copied from data.js. Everything now works as expected.
@peterjanderson672 I just removed the async/await function but its still working for me. No its not needed but I just tried to handle the promise more efficiently before returning the item.
@shantanukumar847 Hold on. We have not got it fixed. I have added the missing semi colons, added ‘async’ and ‘await’ to the hook, and I still see the same problem
What this is saying is that the item that is returned to the insert function needs to be a Promise. Making the function Async kind of makes that happen but adding await in front of a String assignment isn’t needed.
The issue is when you encounter an error or exception in this function you need to force the correct behaviour. The API says you need to return a rejected promise - which is your clue :-).
To force the result to be a Promise the easy thing to do is return a Promise.resolve(). @shantanukumar847 essentially forced a Promise wrapper on the function by adding async.
So your return code if changed to
return Promise.resolve(item);
Will probably do the trick.
Steve
P.S.
The async function declaration defines an asynchronous function, which returns an AsyncFunction object. An asynchronous function is a function which operates asynchronously via the event loop, using an implicit Promise to return its result. But the syntax and structure of your code using async functions is much more like using standard synchronous functions.
Made a simple change to the hook. Same problem. Look at my site and you will see the new item I added using the dashboard editor has fired the hook peterjanderson6724.wixsite.com/website-1
@stevendc Appreciate your input. So why would data.js code that has been working for months suddenly stop working? When someone activates a data hook in the Data Manager, Wix generates the shell code for that hook in data.js. If a promise resolution is needed, then shouldn’t that code be generated? Clearly, something changed inside the Wix wrapper code for Live sites that now requires the promise resolution.
@peterjanderson672 I’m not sure I understood you correctly. I see that your site is now working as expected (the testField is BBBB), am I missing anything?