Custom query logics in controller not working

System Information
  • Strapi Version: v4.9.0
  • Operating System: Mac OS M2
  • Database: MySQL
  • Node Version: v18.15.0
  • NPM Version: 9.5.0
  • Yarn Version:

I’m new to Strapi, I’ve spent two days trying to solve this issue but I can’t find a solution. The logic is simple, remove the “body” field when “premium” boolean is true and the user role ID is not 11. Here’s my code:

"use strict";

const { createCoreController } = require("@strapi/strapi").factories;

module.exports = createCoreController("api::content.content", {
  async find(ctx) {
    if (ctx.state.user.role.id !== 11) {
      ctx.query = {
        ...ctx.query,
        fields: ["title", "caption"],
        where: {
          premium: true,
        },
      };
    } else {
      ctx.query = { ...ctx.query };
    }

    const { data, meta } = await super.find(ctx);

    return { data, meta };
  },
});

Where you have specified where, you would want to have filters. The logic isn’t quite right, see Filtering | Strapi Documentation for more details.

Also note, ctx.query = { ...ctx.query }; is redundant, just skip the else { ... } entirely.