[Strapi v4] - strapi.entityService.findMany - populate user id

Hello,
how can I get the relation to a user or other table.
I’m trying to get the IDs of users who have records, but I can’t access them.
If I use “*” it takes all the fields and I only need the user ID

 async function createEnergy() {
        //console.log("Mining");

        const fabric = await strapi.entityService.findMany(
          "api::shop-product.shop-product",
          {
            fields: ["id"],
            filters: { status: "used" },
            populate: "users-permissions.user",
          }
        );

        console.log(fabric);
      }
    },

Update:
I managed to get an array with the user, but is there any way I can filter only the ID

const fabric = await strapi.entityService.findMany(
          "api::shop-product.shop-product",
          {
            fields: ["id"],
            filters: { status: "used" },
            populate: ["users_permissions_user"],
          }
        );

I’m new to strapi and currently working my way through lots of tutorials, fighting through older obsolete stuff trying to work out the new way to do things. I’m doing this via a custom handler on a controller but the principle should be the same for your filtering. You need to define a route that hits this as well of course.

I’ve included the full controller code because I’ve spent about 4 hours piecing this together and it might save others the pain as there’s a ton of older stuff.

'use strict';
const utils = require("@strapi/utils");
const {sanitize} = utils;

const sanitizeOutput = (data, ctx) => {
  const schema = strapi.getModel('api::event.event');
  const {auth} = ctx.state;
  return sanitize.contentAPI.output(data, schema ,{auth});
};

/**
 *  event controller
 */

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

module.exports = createCoreController('api::event.event', ({strapi}) => ({
  me: async (ctx, next) => {
    const user = ctx.state.user
    console.log(user)
    if (!user) {
      return ctx.badRequest(null, [{messages: [{id: "No auth header found"}]}])
    }

    const data = await strapi.entityService.findMany("api::event.event", {
      populate: 'users_permissions_user',
      filters: {
        "users_permissions_user": {
          "id": user.id
        }
      },

    });

    if (!data) {
      return ctx.notFound();
    }

    const sanitizedEvents = await sanitizeOutput(data, ctx);

    return (sanitizedEvents);
  }
}));

The filtering you need should be as above via the nested object. i.e. like this, with use actual user.id coming from wherever you have it, in the controller I get it as above.

 filters: {
        "users_permissions_user": {
          "id": user.id
        }
      },

The sanitization will remove the user data from the returned json, so you include it in order to filter, then remove it before returning based on your needs. You can confirm this if you omit the {auth} from return santize.contentAPI.output, in which case your user data will also be returned from the api endpoint.

Hopefully it helps someone else

1 Like