Update model content in single component via lifecycle hook

System Information
  • Strapi Version: 4.24.4
  • Operating System: Ubuntu
  • Database: sqlite
  • Node Version: node v18.19.1
  • NPM Version: —
  • Yarn Version: 1.22.22

Hello everybody,

we’ve been using Strapi “GUI-only” for quite a while now. Now we have project which forces us to do some backend modifications and we’re kind of stuck. Our case:

We have a model “case” with some basic properties and nested components (repeatable + single components) inside. One single component is “address”, which has properties “city”, “postal code” etc. My question is: How is it possible to update one or more properties of this single component during a lifecycle hook? Specifically I want to update another field (“federal state”) with the results of an Openstreetmap query. I tried several approaches like

a) direct assignement via

  async beforeUpdate(event) {
    const data  = event.params.data;

    data.address = {
      ...data.address,
      federal_state: 'foo'
    }
  }

which does not seem to work. Next approach was via b) entityService (more or less) like so:

      await strapi.entityService.update('api::case.case', currentCase.id, {
        data: {
          address: {
            ...currentCase.address,
            federal_state: 'foo'
          },
        },
      });

which also did not seem to work. I do not find any really helpful hints in the official documentation. Is there sort of an official way for this? Modifying the direct properties of the case model works fine!

thanx, Andi