System Information
- Strapi Version: v4.9.0
- Operating System: macOS 13.2.1
- Database: MySQL
- Node Version: 18.15.0
- NPM Version: 9.5.0
- Yarn Version:
I’m customizing a controller (in Strapi V4). Using filters, I’m trying to ignore entires where if at least one of the items in the “userFriends” array is in the “friends” relational field. My code below only works if “friends” only contains one item, unless the “userFriends” array matches “friends” relational fields.
Here’s my code, I’d truly appreciate help with this as I’m new with Strapi and couldn’t find guidance for this case in the documentation/community.
async find(ctx) {
const id = ctx.state.user.id;
const user = await strapi.entityService.findOne(
"plugin::users-permissions.user",
id,
{ populate: { friends: true } }
);
const userFriends = user.friends.map((friend) => friend.id);
if (userFriends) {
ctx.query = {
...ctx.query,
filters: {
friends: {
id: {
$notIn: userFriends ,
},
},
},
fields: ["nickname", "ranking"],
};
} else {
ctx.query = { ...ctx.query, fields: ["nickname", "ranking"] };
}
const { data, meta } = await super.find(ctx);
return { data, meta };
}