I recently started a strapi v5 project, and faced the same issue as in my other strapi projects, that at some point I must populate queried data with the id of the current user.
More precisely, I want to create an instance of “Team” which has a FK to the users-permissions-user which I want to set to the current user making the request.
And in v5 as in the versions before this seems to be unnecessarily complicated. As of v5 strapi recommends using lifecycle hooks instead of the entityService to create this relation. I’ve tried variations of the following:
const {data} = ctx.request.body;
const userId = ctx.state.user?.id;
if (!userId) {
return ctx.badRequest("User not authenticated");
}
const response = await strapi.entityService.create(
"api::team.team",
{
data: {
...data,
user: userId
}
}
);
return {response};
also this:
let team = await strapi.documents('api::team.team').create({
data: {
teamName: 'asdasd',
members: ['asdasd', 'asasd'],
user: 11 // should be the userid, hardcoded for debugging purposes
}
});
Can anyone help me achieving this?
Greetz
DeM