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

113
Visualizações
strapi 4 populate when update

Is there an way to populate the field when updating,

...
 const response = await cartService.update(id, { data: { items } });
 const sanitizedEntity = await this.sanitizeOutput(response, ctx);
 return this.transformResponse(sanitizedEntity);


about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can do this, by attaching the populate=[your_relational_field] as the query string in the http PUT request call.

Sample request

http://localhost:1337/api/cart/2?populate=category

Sample request body in JSON format
{
   "data": {
        "items": "items data here"
   }
}

That's all! You don't even need to override the core update method in your controller and the query string will directly be picked up by StrapiV4. But in case for some reason you've overridden the core update method from the controller, then you can simply pass the ctx instance to the core update or findOne methods like below:

"use strict";

/**
 *  cart controller
 */
const { createCoreController } = require("@strapi/strapi").factories;

module.exports = createCoreController("api::cart.cart", ({ strapi }) => ({
  async update(ctx) {
    // let's say you've written some custom logic here
    // finally return the response from core update method
    const response = await super.update(ctx);
    return response;

    // OR
    // You can even use the core `findOne` method instead
    const response = await super.findOne(ctx);
    return response;

    // OR
    // if you've used some other service then you can pass in the populate option to the update method
    const response = await cartService.update(id, {data: { items }, populate: "items.product" });
    return response;
  },
}));

about 4 years ago · Juan Pablo Isaza Relatório
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