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

@peterjanderson672 That may be a different problem - but your original problem of hooks not executing is a bug on our system. We will work to fix it.
EDIT: To be clear, there is a workaround (what I suggested above), we will work to fix it so it won’t happen again.

@tomer-wix Thank you for confirming that there is a bug.

@davebogan98 We knew it was a bug!

@tomer-wix Just to be clear - your workaround of making a small change to the hook does not solve the problem.

@davebogan98 A good question and probably a reasonable assertion. I would look at the code and see if the result being returned is itself a Promise or returned from a Promise. So for example if the hook does something like:

export function dataCollection_beforeInsert(item) {
    return wixData.query("dataCollection")
    .eq('keyField', item.keyField)
    .find()
    .then((result)=> {
        // do something with result and item
        return item;
    });
}

Then the item is automagically wrapped in a Promise when returned after the // do something with result and item has completed.

Its a big question which is hard to answer without some context :slight_smile:

@stevendc I think part of the struggle here is that we are dealing with a level of code abstraction. In page code, one calls a wixData function, not the data hook function. The Wix wrapper code should (as Tomer acknowledges) execute the data hook and resolve any promises. That is why there is an onFailure data hook – to catch when another data hook does not resolve properly. I have seen that onFailure hook more times than I care to count because of my own errors. ;-).

The problem has returned in the last ten minutes

@peterjanderson672 according to our records the workaround fixed the root cause in your site. You shouldn’t need to wrap the item in a resolved promise, you can try to remove it and see if the site still
works

I think you are right. Sometimes it is always good to go back to the API and validate your code. Sometimes the examples provided (used as a template) don’t always follow the API syntax but work anyway. The challenge is that javascript is incredibly forgiving because it doesn’t type check until the last moment of execution. This makes debugging hard especially if the code is in the backend.

@tomer-wix Could you confirm that your workaround is to make a trivial edit to the hook function?

Did you publish your site again with other changes?
EDIT: we are regardless working to fix the problem in the background.

@tomer-wix I have just edited the hook as follows, and republished. Same problem.

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

@peterjanderson672 Make any edit to the data.js, doesn’t matter where.
I’m continuing the conversation in the other thread.

@peterjanderson672 Thanks, we are still checking, I appreciate your patience.

[@Tomer (Wix)] The docs clearly say that a Promise should be returned not that one is automatically wrapped around the result. Peter just said using a Promise resolve works (I think). So are you saying the docs are also wrong?

@stevendc Your fix was working, and now seems to have stopped working. I think i will focus on Brexit instead.

@stevendc Thanks Steve, I’ll try to elaborate a bit on what I meant:

  1. According to the docs, you should return a promise, you are correct. Actually, in the code examples we don’t return a promise and we probably need to fix that (I’ll take care of that later with our documentation team).
  2. Due to the nature of JS, you probably don’t have to return a promise (even if we document that you do), assuming it is wrapped further down the chain. That’s why existing code without promises works just fine. Although I always recommend to stick to the docs :slight_smile:
  3. Peter’s site has a different problem of hooks not executing, due to an issue on our side unrelated to the actual code written inside data.js. We identified the root cause and working to fix it.

Let me know if you have further questions, I’d love to answer whatever I can :slight_smile:

Cool - yes understand the Promise chaining which works if you start off in a Promise. Sounds like you have it in hand.

[@Peter Anderson] Ha ha good luck with that. Whenever I have tried that I leads to other headaches!

Hi everyone, the fix is fully deployed.
Thank you again for your patience, we truly appreciate it!