Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

597
Visualizações
Strapi v4: How to access context within a service

When I create a custom controller in Strapi I get convenient access to a Context object where I can get the current user and use that user's data as I wish:

module.exports = createCoreController("api::event.event", ({ strapi }) => ({
  //Get logged in  users
  async me(ctx) {
    const user = ctx.state.user;

    if (!user) {
      return ctx.badRequest(null, [
        { messages: [{ id: "No authorization header was found" }] },
      ]);
    }
    const data = await strapi.entityService.findMany("api::event.event", {
      filters: {
        user: user.id,
      },
    });

    if (!data) {
      return ctx.notFound();
    }

    const sanitizedEntity = await sanitize.contentAPI.output(data);

    return { data: sanitizedEntity };
  },
}));

However when I create a custom service by trying to extend the Core services, I don't seem to have the context object as above:

module.exports = createCoreService("api::event.event", ({ strapi }) => ({
  //https://docs.strapi.io/developer-docs/latest/development/backend-customization/services.html#extending-core-services

  async create(params) {
    console.log("inside event.js - create");
    console.log("params", params);

    console.log("params to save", params);

    // some logic here
    const result = await super.create(params);
    // some more logic

    return result;
  },

  async update(entityId, params) {
    params.data.user = entityId;

    // some logic here
    const result = await super.update(entityId, params);
    // some more logic

    return result;
  },
}));

If possible I would like to access the context object because I would like to access the user info and get user id and add that id as the owner or creator of that entry.

Is it possible and how does one do that???

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda