1

I have a strapi/nextjs blog that consists of collection types called Article. I wrote a lifestyle hook to update the count everytime the article is fetched. The issue is that both fetching the article and updating the view count triggers the lifestyle hook, and I can't figure out how to separate the two. Here is the hook:

// location: './api/article/content-types/article/lifeycles.js'

module.exports = {
  async afterFindOne(event) {
    if (event) {
      const { result } = event;
      const currentViews = parseInt(result.views);
      const newView = currentViews+1
      strapi.query("api::article.article").update({
        where: { id: result.id.toString() },
        data: {
          views: newView,
        },
      });
    }
  },
};

I'd like to avoid custom controllers if I can and Im not sure there's anything in the "event" parameter that will allow you to distinguish what triggered the hook.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.