如何在 Strapi 中仅返回选定的某些字段?

And*_*ing 10 strapi

非常简单(我希望)。我希望能够使用 API 端点并让它只返回指定的字段。IE类似这样的东西

http://localhost:1337/api/reference?select=["name"]
Run Code Online (Sandbox Code Playgroud)

理想情况下会返回某种形式的东西

[{"name": "Ref1"}]
Run Code Online (Sandbox Code Playgroud)

不幸的是,情况并非如此,实际上它返回以下内容。

http://localhost:1337/api/reference?select=["name"]
Run Code Online (Sandbox Code Playgroud)

如果我决定一次加载 10、20、30 或更多记录,这在任何现实世界中都会立即成为问题,我最终加载了所需数据的 50 倍。更多的带宽用完,加载时间变慢等。

ido*_*off 7

我是如何解决这个问题的:

  1. 在contributor/controllers/contributor.js 中创建自定义控制器操作(例如,'findPaths')
    module.exports = {
       findPaths: async ctx => {
           const result = await strapi
               .query('contributor')
               .model.fetchAll({ columns: ['slug'] }) // here we wait for one column only
           ctx.send(result);
       }
    }
Run Code Online (Sandbox Code Playgroud)
  1. 在contributor/config/routes.json 中添加自定义路由(例如'paths')
    {
      "method": "GET",
      "path": "/contributors/paths",
      "handler": "contributor.findPaths",
      "config": {
        "policies": []
      }
    },
Run Code Online (Sandbox Code Playgroud)
  1. 在管理面板中为贡献者实体、路径操作添加权限

就是这样。现在它只显示slug所有贡献者记录中的字段。

http://your-host:1337/contributors/paths
Run Code Online (Sandbox Code Playgroud)


Pie*_*rre 1

Strapi 中尚未实现此功能。作为补偿,最好的选择可能是使用 GraphQL ( http://strapi.io/documentation/graphql )。

请随意创建问题或提交拉取请求:https://github.com/wistityhq/strapi