System Information
- Strapi Version: v4.20.4
- Operating System: Windows
- Database: mysql
- Node Version: v20.10.0
- NPM Version: 10.2.4
- Yarn Version: 1.22.19
Hi , I have an collection named Profile and it has a component DateOfBirth and Age , i want to calculate the Age dynamically and store it in the field. I used lifecycle to do this and it works :
const { differenceInYears, parseISO } = require('date-fns');
module.exports = {
beforeCreate(event) {
if (event.params.data.DateOfBirth) {
event.params.data.Age = calculateAge(event.params.data.DateOfBirth);
}
},
beforeUpdate(event) {
if (event.params.data.DateOfBirth) {
event.params.data.Age = calculateAge(event.params.data.DateOfBirth);
}
},
};
function calculateAge(dateOfBirth) {
const today = new Date();
const birthDate = parseISO(dateOfBirth);
return differenceInYears(today, birthDate);
}
But now my issue is later if i don’t update this data still the Age will be same for years even though practically their age is changed. I searched around and found afterFind but using it gives error : │ │ Error: Content Type Definition is invalid for api::profile.profile'. │ │ lifecycles field has unspecified keys: afterFind │ │ at Module.createContentType (D:\Project\node_modules\@strapi\strapi\dist\core\domain\content-type\index.js:21:13) │ │ at Object.add (D:\Project\node_modules\@strapi\strapi\dist\core\registries\content-types.js:53:35) │ │ at Object.load (D:\Project\node_modules\@strapi\strapi\dist\core\domain\module\index.js:56:45) │ │ at Object.add (D:\Project\node_modules\@strapi\strapi\dist\core\registries\modules.js:18:26) │ │ at Object.add (D:\Project\node_modules\@strapi\strapi\dist\core\registries\apis.js:16:51) │ │ at Object.loadAPIs (D:\Project\node_modules\@strapi\strapi\dist\core\loaders\apis.js:31:34) │ │ at async Strapi.loadAPIs (D:\Project\node_modules\@strapi\strapi\dist\Strapi.js:317:5) │ │ at async Promise.all (index 5) │ │ at async Strapi.register (D:\Project\node_modules\@strapi\strapi\dist\Strapi.js:341:5) │ │ at async Strapi.load (D:\Project\node_modules\@strapi\strapi\dist\Strapi.js:425:5)