New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
populateCreatorFields not working in Strapi v4 #12228
Comments
some workaround? |
Here is some workaround. It doesn't need to set "populateCreatorFields" options. You can use strapi db query and ask to populate createdBy field. If you use Graphql API, you need to extend the query you want // path: ./src/index.js
module.exports = {
register({ strapi }) {
const extensionService = strapi.plugin('graphql').service('extension');
const extension = ({ nexus }) => ({
types: [
// creating new object type called Creator
nexus.objectType({
type: 'Creator',
name: 'Creator',
definition(t) {
t.int('id');
t.string('firstname');
t.string('lastname');
},
}),
nexus.extendType({
type: 'Article',
definition(t) {
// we want to know who is the creator
t.field('createdBy', {
type: 'Creator',
async resolve(root, args, ctx) {
// when we use query, we can populate createdBy
const query = strapi.db.query('api::article.article');
const article = await query.findOne({
where: {
id: root.id,
},
populate: ['createdBy'],
});
return {
id: page.createdBy.id,
firstname: page.createdBy.firstname,
lastname: page.createdBy.lastname,
};
},
})
}
}),
],
});
extensionService.use(extension);
}
} When I query articles, I can ask for the createdBy query {
articles {
data {
attributes {
createdBy {
id
firstname
lastname
}
}
}
}
} If you use REST API, you can extend the controller 'use strict';
/**
* article controller
*/
const { createCoreController } = require('@strapi/strapi').factories;
module.exports = createCoreController('api::article.article', ({ strapi }) => ({
async find(ctx) {
// Calling the default core action
const { data, meta } = await super.find(ctx);
const query = strapi.db.query('api::article.article');
await Promise.all(
data.map(async (item, index) => {
const article = await query.findOne({
where: {
id: item.id,
},
populate: ['createdBy'],
});
data[index].attributes.createdBy = {
id: page.createdBy.id,
firstname: page.createdBy.firstname,
lastname: page.createdBy.lastname,
};
})
);
return { data, meta };
},
})); |
This issue has been mentioned on Strapi Community Forum. There might be relevant details there: https://forum.strapi.io/t/cant-get-createdby-from-api/15687/2 |
This is correct, we don't populate anything by default so you need to specify the population in v4 |
@derrickmehaffy the last version of the docs reads:
English is not my main language so maybe I just don't understand the sentence correctly. To me, it means if populateCreatorFields is true, then the created_by and updated_by fields will be included in the API response. Did I misunderstood? If I misunderstood, then would you please clarify what is the real meaning of populateCreatorFields? Also, if populateCreatorFields does not allow to include created_by and updated_by in the API response then what is the 'official' way of doing it (besides using fancy workarounds like the ones above)? |
This is a legacy setting from Strapi v3 which automatically populated those fields, in v4 this is no longer an option so this setting I believe now only allows you to populate them at query time. We have no method to auto populate anything in Strapi v4, so this documentation probably could be updated to reflect that. |
I know this issue is closed but, is the creator fields issue solved in version 4.* ? I'm trying it using the REST API and those fields are not appearing on the response. |
Hello maintainers and @derrickmehaffy of this documentation. I see that this issue is still confusing some people who are coming in new to Strapi (as was I) so after I figured it out, I thought I should just help you guys update the documentation a bit to reflect the steps better for v4. So I have made the necessary changes in my local environment but I am not completely sure how to proceed with the push request after that (this is my first time doing a contribution :) ) That's the link to the fork I have worked on, you guys can check the changes I have made in the commit. And then maybe instruct me what to do next.. |
Can you open a PR against the documentation repo 🙏 |
how what and where I can specify to get the |
wow and I just found the createdBy isn't even regular user who signed up e.g. via google, thats sooo confusing ;( |
I created a I do see in Strapi admin panel on the right side "author: Somename" but even with query parameter populate=* I still don't receive author and createdAt fields |
Extending the controller works and returns WHY?
it has a performance impact so it is not the proper solution. Proper solution would be to tell |
Bug report
Describe the bug
Creator fields are not returned by Strapi V4 REST API even when
populateCreatorFields
is set to true.Steps to reproduce the behavior
npm run develop
name
fieldfind
andfindOne
on your collection and save)options
insrc/api/<collection>/content-types/<collection>/schema.json
to the following:Expected behavior
name
field is not returned anymorecreated_by
field is returnedActual behavior
name
field is not returned anymorecreated_by
field is not returnedSystem
The text was updated successfully, but these errors were encountered: