Problem with custom script when calculating the value in the field of the child table

I have a “reg_inc” doctype.
it has a field “table” - a child doctype called “test_child_table”.
there are three fields in the table “first_data”, “sec_data” and “trhe_data”.
I’m trying to write a custom script to calculate the value of “sec_data”.
The script works, but not as expected.
When the first time click on “save”, the calculation of “sec_data” is not performed.
If, after saving, “first_data” is changed again, then the script is executed.
What could be the problem?

frappe.ui.form.on (‘reg_inc’, {
refresh (frm) {

frappe.ui.form.on (“test_child_table”, “first_data”, function (frm, cdt, cdn) {
var d = locals [cdt] [cdn];
d.sec_data = d.trhe_data * d.first_data;
})
}
})

Try with this:

frappe.ui.form.on (‘reg_inc’, {
refresh (frm) {}
});

frappe.ui.form.on (“test_child_table”, “first_data”, function (frm, cdt, cdn) {
    var d = locals [cdt] [cdn];
    var result = d.trhe_data * d.first_data;
    frappe.model.set_value(cdt, cdn, "sec_data", result);
});

or even better: add a hook before_save() in python. This way you’re sure that this code will be executed

thanks for the answer.
But your code works the same way as mine.
The first time nothing happens. Calculation takes place preservation and then, after repeated
changing values ​​(same as in my code).
But for experiment I changed the line
var result = d.trhe_data * d.first_data;
per line
var result = 3 * d.first_data;
and the calculation takes place immediately.
I guess that the initial error is due to the fact that at first the trhe_data field is not filled, and therefore the calculation does not occur.
And after saving it already has a value.
How can I solve this problem?

ok,
I solved this problem.

frappe.ui.form.on(‘reg_inc’, {
refresh(frm) {
}
});

frappe.ui.form.on (“test_child_table”, “first_data”, function (frm, cdt, cdn) {
var d = locals [cdt] [cdn];
var rez = d.first_data*100/d.trhe_data;
frappe.model.set_value(cdt, cdn, “sec_data”, rez);
});

frappe.ui.form.on (“test_child_table”, “trhe_data”, function (frm, cdt, cdn) {
var d = locals [cdt] [cdn];
var rez = d.first_data*100/d.trhe_data;
frappe.model.set_value(cdt, cdn, “sec_data”, rez);
});

Big thank you again!