Can I update user model from different model life cycle

System Information
  • Strapi Version: 3.3.3
  • Operating System: Windows
  • Database: Mongodb
  • Node Version: 12.19
  • NPM Version: 6.13
  • Yarn Version: 1.22.4

Hi, I am wanting to edit a user field on Strapi during a model life cycle.

If a certain value changes in my model I want to update the owner’s (user) data. I have the lifecycle working as I want but can’t figure out how to update the user through the model file. Any help on how I can do this or if this is possible from model lifecycles?

module.exports = {
  lifecycles: {
    async afterUpdate(result, params, data) {
      //model was updated, now you can query the user's model to update it also.
      await strapi.query('user', 'users-permissions').update({ id:data.user }, {amount:10});
      //update function accepts two parameters, first one is used to identify the user and the second one is the Data that you want to update.
    },
  },
};
1 Like

Thanks! I just found a similar answer on another post that you sunnyson answered. :grinning:

1 Like

Follow-up question:

Do updates like these HAVE to happen in model lifecycles? Or is it also possible to do that in the controller?