Background:
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?
@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 item.avgClb=avg1 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 item.avgCmn=avg2 let avg3=((11-item.ind01)+(11-item.ind02)+item.ind03+(11-item.ind04)+item.ind05)/5 item.avgInd=avg3 let avg4=(item.ldr01+(11-item.ldr02)+(11-item.ldr03)+item.ldr04+item.ldr05)/5 item.avgLdr=avg4 let avg5=(item.ogz01+(11-item.ogz02)+item.ogz03+(11-item.ogz04)+(11-item.ogz05))/5 item.avgOgz=avg5 let avg6=(item.wlb01+item.wlb02+(11-item.wlb03)+item.wlb04+(11-item.wlb05))/5 item.avgWlb=avg6 return item }
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
https://www.w3schools.com/jsref/jsref_tofixed.asp
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.