How can I return selected fields from content types and also use lifecycle hooks?

System Information
  • Strapi Version: 3.5.3
  • Operating System: macOs Catalina 10.15.7
  • Database: MongoDB
  • Node Version: 14.16.1
  • NPM Version: 6.14.12
  • Yarn Version:

I have a flow model with the following fields, which I want to return only specific fields in find query:

  • name
  • description
  • active
  • flowJson
  • nodesConfig

I also populate an extra property to each result json in afterFind hook.

This discussion 1115 helped me to select “flowJson” field with projection, as below:

await strapi.query(‘flow’).model.find().select(‘flowJson’)

But the problem is that using strapi.query.model(‘flow’).find() does not run lifecycle hooks. So how would I return specific fields, while also running lifecycle methods?

Which lifecycle are you using there?

“afterFind” hook

Did you debug it?

Yes, I did. But the afterFind hook won’t trigger if I use strapi.query.model.find().

Is there any way to return selected fields using strapi.services?

1 Like