0

I'm trying developer an ecommerce API and I have a question:

What's the difference between the super.find() and a strapi.entityService?

I have a product controller and I changed the find controller:

products/controller/products.js


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

module.exports = createCoreController("api::product.product", ({ strapi }) => ({
  async find(ctx){

    const data = await super.find(ctx);

    ...
    //const data = await strapi.entityService.findMany("api::product.product");

    return data
}

Inside the function has a super.find and then has returning a specific structure of JSON, like this:

{
    "data": [
        {
            "id": 8,
            "attributes": {
                "name": "Fiat Marea 20V ",
                "sku": "FT550",
                "description": "<p>Apenas uma descrição de teste Produto único topzeira&nbsp;</p><p>&nbsp;</p><p><strong>Altura</strong>: 12&nbsp;</p><p><strong>Largura</strong>: 12&nbsp;</p><p><strong>Produnfidade</strong>: 12</p>",
                "price": 19900,
                "status": true,
                "createdAt": "2022-12-02T22:23:47.483Z",
                "updatedAt": "2023-01-05T12:31:08.461Z",
                "quantity": 2,
                "price_discount": null,
                "publishedAt": "2023-01-05T12:14:25.342Z"
            }
        }
    ],
    "meta": {
        "pagination": {
            "page": 1,
            "pageSize": 25,
            "pageCount": 1,
            "total": 1
        }
    }
}

The structure of products startes with you id and your attributes but... If I use the await strapi.entityService.findMany("api::product.product") I has this return:

[
    {
        "id": 8,
        "name": "Fiat Marea 20V ",
        "sku": "FT550",
        "description": "<p>Apenas uma descrição de teste Produto único topzeira&nbsp;</p><p>&nbsp;</p><p><strong>Altura</strong>: 12&nbsp;</p><p><strong>Largura</strong>: 12&nbsp;</p><p><strong>Produnfidade</strong>: 12</p>",
        "price": 19900,
        "status": true,
        "createdAt": "2022-12-02T22:23:47.483Z",
        "updatedAt": "2023-01-05T12:31:08.461Z",
        "quantity": 2,
        "price_discount": null,
        "publishedAt": "2023-01-05T12:14:25.342Z"
    }
]

The structures of return is different, but why? how I can receive the same structure of super.find() using the strapi.entityService or strapi.query?

How can I solve this problem?

1 Answer 1

0

super.find() is a wrapping function that has built in pagination, sanitizeEntity and transformResponse.

strapi.EntityService is recommended API to be used in custom controllers and services, it's a higher level API that also handles components and dynamic zones. strapi.db.query is lower level API, that gives unrestricted internal access to the database layer.

reference

To make it more easy, you can think about this as entityService is for creating and updating data, db.query is for finding and filtering data.

So, how to receive response in same data structure as super find:

  async find(ctx) {
    const { query } = ctx;

    const entity = await strapi.entityService.findMany("api::test.test", query);
    const sanitizedEntity = await this.sanitizeOutput(entity, ctx);

    return this.transformResponse(sanitizedEntity);
  },

with simple pagination:

async find(ctx) {
    let { query } = ctx;
    query.offset = query.offset ?? 0;
    query.limit = query.limit ?? 20;

    const entity = await strapi.entityService.findMany("api::test.test", query);
    const sanitizedEntity = await this.sanitizeOutput(entity, ctx);

    let { data, meta } = this.transformResponse(sanitizedEntity);

    return {
      data,
      meta: {
        total: await strapi.db.query("api::test.test").count(query),
        offset: query.offset,
        limit: query.limit,
      },
    };
  },
Sign up to request clarification or add additional context in comments.

4 Comments

where is the function "this.sanitizeOutput", "this.transformResponse"
It’s inherited when u use createCoreController
How can I import this function if I do not use createCoreController sir?
@LêVũHuy create a question provide details where you want this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.