Data is not seen in the lifecycle hooks for the component inside a collection

I try to get the params from the lifecyclehook. The data for the component is not showing. Therefore, I cannot access to the data. Is there available options to reveal the data for the component inside the lifecycle hook event methods.

2 Likes

Hi, I’m stuck with the same problem and it seams like this possibility has been removed in v4.
Btw we are not the only ones having this issue, in fact there is a feature request for this and you can vote for it here

1 Like

I found out another way to get the components data with raw query using strapi.db.connection() in lifecycles hook. I think you can also try like that. There may be another feasible solution for that. Welcome any suggestions.

const user = await strapi.db.connection
  .select("*")
  .from(`${process.env.DATABASE_SCHEMA}.components_userinfo_user_infos`)
  .where({ id: data.userInfo.id });

Thank you for the tip :blush:
It seems a good workaround so I’ll try that way

1 Like

Yes seems pretty daft you can’t access the data easily, but after much digging I found this option to access the data

  async beforeUpdate(event) {
    const { data, where, select, populate } = event.params;
    const ctx = strapi.requestContext.get();
    console.log(ctx.request.body);
  }

Although not sure how much it helps as there no obvious way to alter the data before saving it

3 Likes

I’m getting this error when trying to get the request context just like you did: roperty 'requestContext' does not exist on type 'Strapi'.

Am I missing an import or something?

Which version of strapi are you using?

strapi.requestContext only works with Strapi v4.3.9+.

Unfortunatelly with Strapi v4.4.6 strapi.requestContext returns undefined

Just try

strapi.requestContext.get()

I am using strapi v4.5.0 and although the strapi.requestContext.get() returns me the component data in the beforeUpdate lifecycle hook, it returns me the updated component data whereas I need the component data before it got updated.
So I’m getting the exact same updated component data in the beforeUpdate as well as afterUpdate hooks with strapi.requestContext.get()
Does anyone know how I can get previous component data from lifecycle hook? Please help

1 Like

Ok I started today with Strapi and that was the first thing I tried.

So it is not possible in Strapi to compute field values if they are in components ?
This must be a bug or not? How should I validate field data if just top level fields are respected?

Would be really cool if the Strapi Team could give a short statement how to deal with this very common issue of validating embedded field data.

Thanks :slight_smile:

You would have to get the old data from the database.

On the layer that lifecycles exists components do not exists they are there own content-types so what you want would not be possible.

How ever you can do some from of checking on everything by checking if the new data being send is valid and if needed you could get the old data from the db

I was able to add validations for the fields inside my components by using middleware instead of lifecycle hook as mentioned here.

1 Like

in v4,

you can do something like this to manipulate your data records,

inside any lifecycle methode,

   const { connection } = strapi.db;
    const tableName = 'components_name';
    
    // Fetch all records from the table
    const records = await connection(tableName).select('*');
    // Iterate through each record and update the columns
    for (const record of records) {
      const info = JSON.parse(record.product);

      await connection(tableName)
        .where({ id: record.id })
        .update({
          column1: info.value,
          column2: info.id,
          column3: info.label,
        });
    }
    
    // Optionally, you can fetch and return the updated records
    const updatedRecords = await connection(tableName).select('*');
    
    // You can access the updated records in the updatedRecords variable
    console.log(updatedRecords);

This solutions works!

Could you point me to the documentation of how to add more filters to such a query?

I am looking to programmatically add multiple where clauses.
I am trying to query a table of a component mapping, so it seems like I can’t use strapi’s entityService or queryEngine.

Why do you directly need to request a component. if this is the cease then most likley it needs to be a content-type