Data hook not working on live site; but working OK in Preview

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:

export function TestData_beforeInsert(item, context) {
    item.testField = 'BBBB'
    console.log('Data hook fired', item)
return item;
}

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?

Hi Peter,

I tried your code EXACTLY as it is. I created the same DB Collection, Database columns too.

However it is working for me but I did add an async/await function for the data.js file

The code which works for me is this:

page code

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((results) => {
            $w('#dataset1').refresh();
            console.log('INSERT SUCCESS', results);
        })
        .catch((err) => {
 let errorMsg = err;
            console.log('INSERT FAIL', errorMsg);
        })
}

export function button1_click(event) {
    $w("#dataset1").refresh();
}

data.js

export async function TestData_beforeInsert(item, context) {
    item.testField = await 'BBBB';
    return item;
}

Hey, I tried to reproduce on a new site but it’s working for me. I’ll try to play with your site.

See: https://dudelemonweb.wixsite.com/userid/peter

If its the ‘await’ that solves the problem then perhaps the Wix documentation should make that clear?

Are you suggesting that every assignment in a hook needs an ‘await’ ??

Peter,

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

Can you please try to make a small change in data.js (even adding space), publish your site and try again?

Will do

@shantanukumar847 @davebogan98 @Peter Anderson

You are all circling around the correct answer. I think the key is in the API documentation which isn’t very clear.


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 HOORAY! Your Promise.resolve trick works. Presumably that should be in all our data hooks?

@peterjanderson672 If the API says that is what you should be returning then YES! :wink:
I feel a Top Comment coming on!

@stevendc And it would be helpful if Wix were to update its examples of data hooks I think.

@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 Thanks, we’re checking.

@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?

@tomer-wix Steve Cropper has the answer - see above - I have implemented his solution. That is

return Promise.resolve(item);

in every hook! Thank you Steve.