System Information
- Strapi Version: 4:19.0
- Operating System: MacOs or Ubuntu
- Database: Postgres
- Node Version: 18.19.0
- NPM Version: 10.2.3
- Yarn Version: n/a
Hello,
I am working with Strapi which is used to create articles that represent my content. Strapi admins can login and create an article which all works fine.
On my web frontend, I want readers/users to be able to see a list of articles that are published by a specific author but I am having trouble getting this data from the Strapi API.
Here is what I have done so far:
- I have extended the controller for the Article content type to return the
createdBy
fields. This works and allows me to get the author ID and name when doing a GET to my articles API (http://localhost:1337/api/articles?populate=*
. Below you can see the extended controller.
module.exports = createCoreController('api::article.article', ({ strapi }) => ({
async find(ctx) {
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: article.createdBy.id,
firstname: article.createdBy.firstname,
lastname: article.createdBy.lastname,
};
})
);
return { data, meta };
},
}));
- Created a new request in my frontend service that queries the Article api with a filter for
createdBy
. This appears to work for other attributes such as category, tags, title etc but it does not work forcreatedBy
.
Here is the query URL format I and trying:
http://localhost:1337/api/articles?filters[createdBy][id][$eq]=2
It returns this error in the response:
{
"data": null,
"error": {
"status": 400,
"name": "ValidationError",
"message": "Invalid parameter createdBy",
"details": {}
}
}
Is it possible for me to get all articles this way? I am trying to get articles that have only be createdBy user ID 2 in this example URL. I know that createdBy is exposed as I am able to see it in other GET requests to this article API.
Any help would be much appreciated.
Thanks.