Lifecycle hooks - get previous value

Hello !!! I want to know if there is a possibility to get the previous value when updating some content. Let’s say in beforeUpdate or afterUpdate… What I need is to compare previous and new values, and execute some code only if there are changes in some specific fields.

Thank you!

1 Like

Here you go:

async beforeUpdate(data, model) {
      let { id } = data;
      let existing = await strapi.query('articles').findOne({ id });
      if (existing && existing.name != model.name) {
        //do something when value changed
      }
    },
4 Likes

I wanted to check if a field changed and then call appropriate api.
This worked.
Thank you.

You can check more details on this comment

Hello!!
How to use the existing data from beforeUpdate lifecycle hook in afterUpdate lifecycle hook. Basically I’m creating a new table and will store what was the change that was made, and who made that change?

Edit: I’m using strapi v3

I’m new to Strapi but I believe you can use and share state in beforeXXX and afterXXX lifecycle hooks. In Strapi v4 at least. Don’t know about v3.