NodeJS 实体服务VS super.find()VS查询引擎API,有什么区别?

dkqlctbz  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(164)

我正在尝试开发一个电子商务API,我有一个问题:
super.find()strapi.entityService之间的区别是什么?
我有一个产品控制器,我更改了查找控制器:

产品/控制器/产品. 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
}

函数内部有一个super. find,然后返回一个JSON的特定结构,如下所示:

{
    "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
        }
    }
}

产品的结构从你的id和你的属性开始,但是......如果我使用await strapi.entityService.findMany("api::product.product"),我会得到以下返回:

[
    {
        "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"
    }
]

返回的结构是不同的,但是为什么呢?我如何使用strapi.entityServicestrapi.query接收相同结构的super.find()
我怎样才能解决这个问题?

izj3ouym

izj3ouym1#

super.find()是内置于paginationsanitizeEntitytransformResponse中的 Package 函数。
strapi.EntityService是推荐在自定义控制器和服务中使用的API,它是一个更高级别的API,也处理组件和动态区域。strapi.db.query是较低级别的API,提供对数据库层的不受限制的内部访问。
reference
为了使它更简单,您可以将其视为entityService用于创建和更新数据,db.query用于查找和过滤数据。
那么,如何在与超级查找相同的数据结构中接收响应:

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);
  },

简单分页:

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,
      },
    };
  },

相关问题