View counter in strapi: With lifecycle hooks? But how to use them?

Seems that the following hook is working now:

module.exports = {
  lifecycles: {
    async afterFindOne(result, params, populate) {
      const article = await strapi.query('article').find({ id: result.id });

      await strapi.query('article').update(
        { id: article[0].id },
        { views: parseInt(article[0].views) + 1 }
      );
    }
  }
};
1 Like