/api/users endpoint is not sending relations and media fields

System Information
  • Strapi Version: 4.1.0
  • Operating System: MacOs Monterrey
  • Database: MySQL
  • Node Version: 15.14.0
  • NPM Version: 6.14.15

Seems like the integration between roles & permissions plugin and Cloudinary is not good,
I’m able to populate images from all the custom content types, but for some reason the relations and media fields added to the user’s content type (which is there by default) are not being sent.

I tested with regular fields and those are being sent (I mean text, boolean, and so on…), but media files and relations are not working.

Already tried sending “populate=*”, “populate=avatar” and “populate[0]=avatar” attributes. But I don’t know if this is a population problem or not

I just found the answer guys, there is a bug in the roles & permissions plugin related to population. There is an issue opened on Github that probably will be merged soon: https://github.com/strapi/strapi/issues/11957

You can use this workaround in the meantime:

  1. Open your Strapi app on a code editor. (Make sure your app is not running during the process)
  2. Create a file called 'strapi-server.js’ route ‘src/extensions/users-permissions/strapi-server.js’ if it doesn’t exist and add the following.
// path: src/extensions/users-permissions/strapi-server.js
module.exports = plugin => {
  const sanitizeOutput = (user) => {
    const {
      password, resetPasswordToken, confirmationToken, ...sanitizedUser
    } = user; // be careful, you need to omit other private attributes yourself
    return sanitizedUser;
  };

  plugin.controllers.user.me = async (ctx) => {
    if (!ctx.state.user) {
      return ctx.unauthorized();
    }
    const user = await strapi.entityService.findOne(
      'plugin::users-permissions.user',
      ctx.state.user.id,
      { populate: ['role'] }
    );

    ctx.body = sanitizeOutput(user);
  };

  plugin.controllers.user.find = async (ctx) => {
    const users = await strapi.entityService.findMany(
      'plugin::users-permissions.user',
      { ...ctx.params, populate: ['role', 'avatar'] }
    );

    ctx.body = users.map(user => sanitizeOutput(user));
  };

  return plugin;
};
  1. Note that you have a controller for every endpoint, findOne, find and so on…
    you need to add the fields you want to populate to the 'populate: ’ values as an array of strings. Example:
{ ...ctx.params, populate: ['role', 'avatar', 'my_fields'] }
  1. You need to run a build
npm run build
  1. Start your project
npm run start
  1. Try fetching from the users or users/me endpoint
http://localhost:1337/api/users
3 Likes

Hi @AntonioChagoya I don’t if this helps or may be an alternative. This is specific to components attached to a content type entry. I have a content type called works and attached a component called images. I needed to make a component in order to be able to arrange images.

With all that said I similarly was having problems accessing the media attached to this component.

I resolved it by alter my populate request like this.

http://localhost:1337/api/works/1?populate[images][populate]=*

Yes you are right, this is specific to the /api/users endpoint, which is attached to the Roles & Permissions plugin, so this workaround only works for images population on the Users content-type and it’s relations.

For example, adding an Avatar image field to the Users content-type

This isn’t really a workaround. This solution breaks the ability to pass filters of any kind to the /users/ endpoint.

I just put the basic workaround for it, official Strapi account suggest using that workaround, it is true that prevents filtering on first instance, but you can access the ctx.req and access the query object to add parameters to the endpoint

Awesome! How can I access if from frontend (Ex. NextJs/Typescript) and adding a fallback image as well.