Age from DOB calculation in backend

Dear All,

I have been utilising the calculation found on Wix for calculated data elements and I have followed the steps and copied the code and it constantly create an error within the Data Collection I am having difficulties with understanding why? I have placed this in the data.js file as suggested. any help would be greatly appreciated. @yisrael-wix

import wixData from ‘wix-data’;

export function Details_afterQuery(item, context) {
item.age = ageFromDob(item.dob);

return item;
}
function ageFromDob(dob) {
const now = new Date();
const days = now.getDate() - dob.getDate();
const months = now.getMonth() - dob.getMonth();
let age = now.getFullYear() - dob.getFullYear();

if (months < 0 || (months === 0 && days < 0)) {
age–;
}

return age.toString();
}

What error are you getting? How are you using this?

the error affects the data collection saying “data failed to load” in an alert box when the data collection is loading, it seems to work on one data collection item and then fails to achieve the same outcome for other added items “Customers”. I have followed the wix example but am happy to provide you the URL of my website for you to be able to look at where the code is placed and utilised.