System Information
- Strapi Version: v4.15.4
- Operating System: Windows 11 Home 23H2 (build SO 22631.2715)
- Database: SQLite
- Node Version: v20.8.0
- NPM Version: v10.2.3
- Yarn Version: -
Hi all, I’m very new to Strapi and a self-taught developer so please bear with me, thanks in advance for any help.
I’m on Strapi v4.15.4 (node v20.8.0) and I created 2 collection types named user-test
and article
. user-test
collection type has the following attributes:
"attributes": {
"name": {
"type": "string"
},
"confirmed": {
"type": "boolean"
}
}
In addition to that user-test
collection type has 4 entries:
id | name | confirmed |
---|---|---|
1 | User1 | true |
2 | User2 | true |
3 | User3 | false |
4 | User4 | true |
article
collection type has the following attributes:
"attributes": {
"title": {
"type": "string"
},
"body": {
"type": "blocks"
},
"authors": {
"type": "relation",
"relation": "oneToMany",
"target": "api::user-test.user-test"
}
}
I’d like to filter, when creating/editing an entry in the article
collection type from the admin panel, the entries of the relation field authors
so that only the users for which the attribute confirmed
is true
are shown. In other words, I’d like to see only User1, User2 and User4 as entries of the relation field authors
.
For now, I edited src\api\article\controllers\article.js
in order to filter the entries in the user-test
collection type but I don’t know how to proceed from here. Also, I’m not sure why I cannot see anywhere the output of console.log(confirmedUsers)
.
'use strict';
const { createCoreController } = require('@strapi/strapi').factories;
const { sanitize } = require('@strapi/utils');
module.exports = createCoreController('api::article.article', ({ strapi }) => ({
async getAuthors(ctx) {
// Fetch confirmed users
const confirmedUsers = await strapi.entityService.findMany('api::user-test.user-test', {
filters: {
confirmed: true,
},
});
console.log(confirmedUsers);
},
}));
Any advice is very much appreciated. Thank you.