I’m very new to Strapi and I’m a self-taught developer, so please bear with me if it’s not a very intelligent question.
I’m on Strapi v4.15.4 (node v20.8.0) and I have an article
collection type with 3 numeric attributes number1
, number2
and total
.
What I’m trying to do is to automatically update the total
attribute, summing number1
and number2
, every time number1
or number2
is updated.
So far, I was able to edit src\api\article\content-types\article\lifecycles.js
as follow:
module.exports = {
async beforeCreate(event) {
if (event.params.data.number2) {
event.params.data.total = event.params.data.number1 + event.params.data.number2;
}
},
async beforeUpdate(event) {
if (event.params.data.number2) {
event.params.data.total = event.params.data.number1 + event.params.data.number2;
}
},
};
The code above actually works when I click the ‘Save’ button, when creating/editing an entry in the article
collection type, from the admin panel. However I can’t find a way to make it work right after updating the number1
or number2
attributes.
Any advice is very much appreciated. Thank you.