Get userinfo from createdBy and updatedBy v4

System Information
  • 4.1.2:
  • Ubuntu:
  • PostgreSQL:
  • 16.14.0:
  • 8.3.1:
  • 1.22.17:

Content-Types automatically adds created_by_id and updated_by_id and the documentation plugin adds createdBy and updatedBy to the 200 response schema but they are not included in the json returned.
How can i get these properties to be returned in my json responses?
It would be nice to be able to display the author on content in my frontend (Angular)

In advance THX!

After reading this topic https://forum.strapi.io/t/cant-get-createdby-from-api/15687/2
I was able to add the information to the content-type.

If someone have a better way of doing this feel free to comment.

    async findOne(ctx){
        const { id } = ctx.params;
        const data = await strapi.db.query('api::wiki.wiki').findOne({
            where: {
                url_slug_full: id.replaceAll('%252F','/').replaceAll('%2F','/'),
            },
            populate: ['createdBy', 'updatedBy'],
        });
        const sanitizedEntity = await this.sanitizeOutput(data, ctx);
        sanitizedEntity.createdBy = {
            id: data.createdBy.id,
            email: data.createdBy.email
        };
        sanitizedEntity.updatedBy = {
            id: data.updatedBy.id,
            email: data.updatedBy.email
        };
        return this.transformResponse(sanitizedEntity);
    }
1 Like

With help of @Rolf_Veino_Sorensen I was able to make it work for my use case. I was trying to get a Post (content-type) by a field (URL) and it was necessary to include the author first and last name. The code is above:

module.exports = createCoreController('api::post.post', ({
  strapi
}) => ({
  async findOne(ctx) {
    const {
      id
    } = ctx.params;

    const data = await strapi.db.query('api::post.post').findOne({
      where: {
        URL: id,
      },
      populate: ['createdBy'],
    });

    const sanitizedEntity = await this.sanitizeOutput(data, ctx);

    sanitizedEntity.createdBy = {
      id: data.createdBy.id,
      email: data.createdBy.email,
      fistName: data.createdBy.firstname,
      lastName: data.createdBy.lastName
    };
    return this.transformResponse(sanitizedEntity);
  },
}));
1 Like

After some more time i discovered that with this method I’m not able to use populate=deep plugin (with this endpoint) so I had to add manually all the sub fields necessary at the populate part.

      populate: {
        createdBy: true,
        Image: true,
        posts: {
          populate: {
            Image: true
          }
        },
      },
1 Like

I am with the same problem, but for me I receive in terminal:
Error: Undefined attribute level operator populate
EDIT: I achieved! My problem was:
const entity = await strapi.service(‘api::produto.produto’).findOne({ … }) (I had got it at documentation)
when it should be: const entity = await strapi.db.query(‘api::produto.produto’).findOne({ … })