How to get "createdBy" and "updatedBy" in returned data?

Hello,

I am desperatly trying to get “createdBy” and “updatedBy” in my returned data … I tried querying with the parameter " populate=* " , I tried finding a solution in the documentation, I setup
“options”: {
“populateCreatorFields”: true
}
but none of these seem to make it work for me. When I do a simple GET, i get the collection data, without the “createdBy” and “updatedBy” info… How can I get these ?

Kind regards,

Ivan

1 Like

If you are using strapi v4, you can add this to your url when calling the service to populate all properties

?populate=%2A

Already tried this. Doesn’t work for me … (indeed I am using strapi v4). Another suggestion ? :confused:

Ah, sorry I read your original post wrong. I thought it was createdAt and updatedAt that you were looking for, when you are actually looking for createdBy and updatedBy. I believe there is currently a long running bug with bringing back a user relationship in the api. Don’t remember the specifics (I’ll try to link the ticket when I find it) but I believe there is a sanitize method being called on the api that strips out any of the user data before it sends the response back to you. I think they hadn’t best decided on how to handle these situations when the user object could contain sensitive data. I think the current best way to fix this is to override your api endpoint and modify it to include your user data.

Edit: Link to the issue on github Population does not work for Users in Users-Permissions · Issue #11957 · strapi/strapi · GitHub

1 Like

Ok. It explains a lot why it’s not working. I hope they will be able to fix that any soon…

In the meanwhile, could you help me with overwritting the api endpoint ? I’m really new to Strapi (since yesterday to be accurate), I’ve looked a bit in the documentation but it was not super clear to me …

I’d really appreciate your help ! :slight_smile:

Kind regards,

Ivan

So if you go to the directory:
src/api/YOUR_ENDPOINT_NAME/controllers/YOUR_ENDPOINT_NAME.js. Open up that file and you can override any of the methods on the controller. Controllers have a number of methods that are automatically created when you create an api. These are: find(), findOne(), create(), update(), delete().

For an example, I have an api called article. When I created that endpoint, it automatically created the controller for me in: src/api/article/controllers/article.js

By default, this controller just looks like this:

'use strict';

/**
 *  article controller
 */

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

module.exports = createCoreController('api::article.article');

In here you can override any of those methods I mentioned above, so as an example, if you wanted to override the find() endpoint:

'use strict';

/**
 *  article controller
 */

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

module.exports = createCoreController('api::article.article', ({ strapi }) =>  ({

  // Method 2: Wrapping a core action (leaves core logic in place)
  async find(ctx) {
    // some custom logic here
    ctx.query = { ...ctx.query, local: 'en' }
    
    // Calling the default core action
    const { data, meta } = await super.find(ctx);

    // some more custom logic
    meta.date = Date.now()

    return { data, meta };
  },

}));

That’s pretty much it, the documentation for this is at this link (Backend customization - Controllers - Strapi Developer Docs). It also shows how you can add more methods to your controllers. You can also find the default controller code in the current version in the github repository (strapi/collection-type.js at master · strapi/strapi · GitHub) so you can copy whatever method you want to override to your file and modify it as you need. Hope this helps!

here is the full answer Can't get createdBy from api