Wix-data API not working in data.js hooks?

I have a number of tables used to drive repeating elements on dynamic pages. For ease of data entry I wanted to use a reference on a number of fields. However as it appears that filters on dynamic pages cannot filter on references I have been building filter and sequence columns in a usable format (string or number) from data that is entered into reference fields.

To do this I have been using the data hooks in data.js.

At one point it was possible to access the values in the reference field using the following form:

item.filterColumn = item.refField.filterValue;

However at some point this stopped working - the reference field simply contains the _id from the referenced table as a string.

So I thought I should be able to use the wix-data API to query the referenced table - but I cannot get this to work at all - my data.js:

import wixData from 'wix-data';

export function BusinessDirectory_beforeUpdate(item, context) {
 //TODO: write your code here...
    wixData.query("AdType")
        .find()
        .then( (results) => {
            item.filterBusinessType = JSON.stringify(results);
        } )
        .catch((err) => {
            item.filterBusinessType = err;
        });

    item.displayTitle = item.title.toUpperCase();
}

If this was working I would expect something to end up in item.filterBusinessType however I get nothing and no error message either. The final line is invoked and works.

So now I’m stumped - how can I update data in the table using data from a referenced table…

Any ideas welcome!

Thank you!

Hey sean,
I’m not sure if I understand you correctly, but anyway you should return the item after changing it.
If the code you wrote doing what you asked for, you only need to return the item.
The code should look like this:


import wixData from 'wix-data';

export function BusinessDirectory_beforeUpdate(item, context) {
 
 return wixData.query("AdType")
        .find()
        .then( (results) => {
           item.filterBusinessType =             JSON.stringify(results);
            item.displayTitle = item.title.toUpperCase();
            return item
        } )
        .catch((err) => {
            item.filterBusinessType = err;
        });
}




Bingo! OK this has really helped and I’m now seeing the changes - I’m still trying to get my head around the way the data API works, but I think that’s given me what I need - Thanks!

The frustrating thing is that there appears to have been a behavioural change which affected my original code… The reason I need to use the data API is only so that I can retrieve values from the references.