Calculated Value not matching field type Number

Visitor survey to collect values (using sliders), then calculating average (using data hook) and displaying the calculated average in a text box (not done yet)

Problem: Calculated value in collection shows warning “Value does not match field type Number” with option of convert or clear. If I use convert, I see the number value as expected, but warning doesn’t go away. Field is Avg CLB (avgClb) and type is Number.
Data Hook code:
export function TR002_FREE_ASSESSMENT_afterUpdate(item, context) {
//TODO: write your code here…
let avg=(item.clb01+item.clb02+( 11 -item.clb03)+( 11 -item.clb04)+item.clb05)/ 5
item.avgClb=avg.toFixed( 2 )
return item
Screen shot of the collection:

What am I doing wrong?

Basically, toFixed is a frontend function which converts a number into a string (and that´s your problem), to display data, not to write to a collection.
So get rid of the toFixed inside the hook and move it to the frontend when displaying the avg.

Hi Giri, thanks for the quick reply, but I still have the same warning even if I remove toFixed. I know there are multiple ways to get this resolved, any other thoughts that come to your mind?

Yes, do me a favour and console.log the data type, like so:

console.log("Data type = "+ typeof avg);

assuming that “avg” is now the name without the toFixed

I think, the data hook does not show messages in console log, any other way to ‘step-through’ the code to find datatype? Any way to ‘force’ the calculation to generate datatype ‘Number’?

OK, another observation … if I type in any value in Avg CLB field, that value is automatically overwritten by the correct calculation.

This makes me believe that the afterUpdate hook is running fine!

However, now I see that nothing is written to Avg CLB field automatically. All other fields show data inserted from the sliders, but Avg CLB is left blank after ‘submit’ action??

PS: I tried the hook of afterInsert which didn’t do anything

@psaraph Have you checked your console.log inside Site Monitor?

@giri-zano Got it! Yes, I could see the console in preview mode. I also experimented with different data hooks and the one that did the trick was beforeInsert. Here is the code that worked!

export function TR002_FREE_ASSESSMENT_beforeInsert(item, context) {
//TODO: write your code here…
let avg1=(item.clb01+item.clb02+( 11 -item.clb03)+( 11 -item.clb04)+item.clb05)/ 5
console.log( "BI Avg CLB is " ,item.avgClb)
console.log( "BI Data type for avg and avgClb is " + typeof avg1, " and " + typeof item.avgClb )
let avg2= (item.cmn01+( 11 -item.cmn02)+item.cmn03+item.cmn04+( 11 -item.cmn05))/ 5
let avg3=(( 11 -item.ind01)+( 11 -item.ind02)+item.ind03+( 11 -item.ind04)+item.ind05)/ 5
let avg4=(item.ldr01+( 11 -item.ldr02)+( 11 -item.ldr03)+item.ldr04+item.ldr05)/ 5
let avg5=(item.ogz01+( 11 -item.ogz02)+item.ogz03+( 11 -item.ogz04)+( 11 -item.ogz05))/ 5
let avg6=(item.wlb01+item.wlb02+( 11 -item.wlb03)+item.wlb04+( 11 -item.wlb05))/ 5
return item

Glad you worked it out.

1 Like