Is it possible to restrict lifecycle method to user action and not API ?
Example when a user update an entry via Strapi dashboard I want to run the afterUpdate lifecycle that will update another entry. However via API if I do that it goes into an infinite loop obviously because → user update entry → afterUpdate triggers → update another entry → lifecycle trigger again → infinite loop…
I had the exact same issue as OP, my afterUpdate code would enter into an infinite loop since it updated data into the same item. I solved it by using direct knex.js commands which apparently don’t trigger lifecycle hooks, this should be made clearer in the documentation…
Posting it here in case someone finds this thread with google like I did.
Here’s how:
async afterUpdate(event){
const data = event.params.data
const titre = data.Titre
const date = data.date
const id = event.params.where.id
const relation = await strapi.db.query('api::radio-show.radio-show').findOne({
where:{id: id},
populate:{resident: true}
})
const resident = relation.resident.Name
const toBeSlug = slugify(resident + " "+ titre + " " + dayjs(date).format("DD-MM-YYYY"))
/* If the query engine was used to update the data, we would enter an infinite loop*/
/* The following code doesnt enter the infinite loop because it interacts directly with the database with knex.js */
const update = await strapi.db.connection("radio_shows").where({id: id}).update({slug:toBeSlug})
}
As you can see, it’s a custom slug creating system that uses a relation field as one of its variable.