Issues with lifecycles and dynamic components

System Information
  • Strapi Version: 4.1.12
  • Operating System: Debian
  • Database: PostgresSQL
  • Node Version: 14.18.3
  • NPM Version: 6.14.15
  • Yarn Version: -

Hello here! I’m trying to automatically calculate macronutrients for a recipe but i’m kinda stuck here:

  • The ingredients are dynamic components
  • They have the data i need to calculate the total macronutrients for the recipe
  • I’d like to calculate them on every creation and every update of a Recipe instance, Recipe having the total fields for the macronutrients on first-level
  • I used beforeUpdate lifecycle but i can’t get the datas in event.params.data (image)
    unknown
  • I wanted to try afterUpdate lifecycle but it would trigger infinite loop on lifecycle because i would need to update the instance again

Is there any workaround? Or what am i doing wrong? Thanks in advance

If you are using beforeUpdate lifecycle method, you can fetch the data using the id of the record. you can get the id of the record using event.params.where.id. Once you have all the entries of ingredients, I believe you can calculate the price.

Thanks for your answer.

I tried this solution but it seems that during this lifecycle the datas aren’t sent yet. Hence when I make the query, I get the old version of the instance, before its update.

If I had a way to get the update and populated data value in the lifecycle parameter I could do this

Do you fine any solution?

No, unfortunately.