Trouble with setting up a data hook

I have a collection that has among many other fields, 3 number fields called place, partpts, totalpts. Place and partpts are imported in as numbers, while I would like totalpts to be calculated and stored in the totalpts field. The formula I would like to use is totalpts = partpts + (51 - place).

Here is what my data.js looks like:
export function TxResults_afterInsert(item, context) {
//TODO: write your code here…
item.totalpts = item.partpts + (51 - item.place);

return item;
}

Will this perform the calculations on my existing data in the collection or is there something I need to do for the calculations to be performed? I have been searching through other forum posts but I am not understanding what I am doing wrong. Some video examples would also be great but I haven’t seen any. Thanks in advance

Hi,

You need to use a hook that is triggered before an insert() function. Currently, you try to update a field after the item has been insert to the collection and as a result nothing has changed at your collection.

View the code below:

export function TxResults_beforeInsert(item, context) {
    item.totalpts = item.partpts + (51 - item.place);
 return item;
}

Here you can read more about beforeInsert( ) hook.

Have a nice day and best of luck!
Sapir,

Thanks Sapir. I tried to do that as well but nothing is populated in my collection for the totalpts field. I also tried to display the value in a table and I just see null. Would I need to reimport all of my data since it is beforeinsert?

Hi,

The hook beforeInsert() happened before you are trying to save an item at the collection, and the return is going to be the update object with the correct field’s value that is going to be saved at your collection.
If you wrote all the FiledKey correctly there shouldn’t be a problem.

Please share a link to your site so we can inspect your issue.

Thanks,
Sapir

Thank you Sapir.

@sapirh Hello Sapir, any luck?

Hi Pool,

I viewed your site and I found several problems:
At my points I refer to the “Add Results” page and “TXResults” DB.

1 . The dataset “boater TxResults dataset” is mode’s Read&Write , I didn’t understand why you choose this mode, because I saw that you only insert a new item to the DB “TXResults” and not trying to read from it. As a result, at preview mode, most of your elements include the submit button were disable.
Only after changing the dataset’s mode to be Write-only I could submit.

2 . After pressing the submit button (after changing the dataset’s mode ) I got a validation problem:


Please check what cause this problem (maybe the DB’s fields properties) ,otherwise it makes it very hard to debug your code.

3 .About your DB’s permissions. Only the Admin can create, update and delete item.
As a result while publishing your site, the visitor would not be able to add a new item to your DB.
Moreover ,the page’s permission is Member Only means only member visitor can view it. (I supposed you do it on purpose , it’s not a mistake just remember all of your pages permissions).

4 . On the page, I didn’t see what is the element that connects to the Partpts field. If you mistakably forgot to connect this field, at your hook function you sum an undefined field with item.place .

export function TxResults_beforeInsert(item, context) {
    item.totalpts = item.partpts + (51 - item.place);

 return item;
}

Please go throw those things and try again to add your result.
Contact us, if you still have a problem and mention In which page at your site the problem is,

Have a nice day and best of luck!
Sapir

Sapir,

The page I have partpts field linked to is the Results page. The Add Results page is not done and not functional, please disregard that page. I want the totalpts field to be calculated automatically and stored in the dataset regardless of how the records are entered. The majority of my records in the collection will be imported via CSV file. I plan on adding more calculated fields but would just like to start with this example.
What is the best way to accomplish this?

Hi pool,

First, importing data will not trigger beforeUpdate() function hook, but it will trigger beforeInsrt(). As a result, If you wish that the field totalpts will automatically be calculated after importing the data, all the data that is related to the calculation (item.place and item.partpts) should be in the CSV file, since this hook function can’t use data from fields that their data is already exists at the database.(The beforeInsert() hook function take the total new object and changes only it’s elements).

For example, see this CSV file:


With this hook function:

export function TxResults_beforeInsert(item, context) {    
      item.totalpts = item.partpts + (51 - item.place); 
      return item; 
}

After the importing the data, this is how my database looks like:


Secondly, if some of the database fields get their data while importing the CSV file and some from another source (like user input) , you will have to write in code a function that will do this calculation, or after all the data is insert use the hook function beforeUpdate( ). (trigger this function by writing an Update() operation at your code)

Best of luck!
Sapir

Thank you for the reply Sapir. In your example, your totalpts field shows 9, but it should show 52 if you used the same formula that I did because it would be 2 + (51 - 1).

Is there a hook function that does calculations on data if I manually entering the data directly into the collection itself (not via a submit form)?

Hi Pool,

If you want that the field totalpts will automatically be calculated after manually entering the data to the database, use the hook function beforeUpdate( ).

export function TxResults_beforeUpdate(item, context) {   
        item.totalpts = item.partpts + (51 - item.place);  
        return item; 
}

The process will be:

  • Entering to your database the fields partpts and place .
  • Press on the plus button to create new item or focused on the totalpts field and press enter.
  • The hook function is triggered and the field totalpts will be updated.

Good Luck!
Sapir,

2 Likes

This appears to be working. Thank you Sapir!

Data hooks are not performing reliably. Within the last two weeks data hooks have stopped firing on insert. It has compromised my live site. When can we rely on data hooks again?