How do I return only selected certain fields in Strapi?

后端 未结 5 1533
独厮守ぢ
独厮守ぢ 2021-01-18 06:16

Pretty straightforward (I hope). I\'d like to be able to use the API endpoint and have it only return specified fields. I.E. something like this

http://local         


        
相关标签:
5条回答
  • 2021-01-18 06:49

    How I solved this:

    1. Create custom controller action (for example, 'findPaths') in contributor/controllers/contributor.js
        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);
           }
        }
    
    1. Add custom route (for example 'paths') in contributor/config/routes.json
        {
          "method": "GET",
          "path": "/contributors/paths",
          "handler": "contributor.findPaths",
          "config": {
            "policies": []
          }
        },
    
    1. Add permission in admin panel for Contributor entity, path action

    That's it. Now it shows only slug field from all contributor's records.

    http://your-host:1337/contributors/paths
    
    0 讨论(0)
  • This feature is not implemented in Strapi yet. To compensate, the best option for you is probably to use GraphQL (http://strapi.io/documentation/graphql).

    Feel free to create an issue or to submit a pull request: https://github.com/wistityhq/strapi

    0 讨论(0)
  • 2021-01-18 06:55

    You can use the select function if you are using MongoDB Database:
    await strapi.query('game-category').model.find().select(["Code"])
    As you can see, I have a model called game-category and I just need the "Code" field so I used the Select function.

    0 讨论(0)
  • 2021-01-18 07:01

    I know this is old thread but I just run into exactly same problem and I could not find any solution. Nothing in the docs or anywhere else.

    After a few minutes of console logging and playing with service I was able to filter my fields using following piece of code:

    const q = Post
      .find()
      .sort(filters.sort)
      .skip(filters.start)
      .limit(filters.limit)
      .populate(populate);
    
    return filterFields(q, ['title', 'content']);
    

    where filterFields is following function:

    function filterFields(q, fields) {
      q._fields = fields;
      return q;
    }
    

    It is kinda dirty solution and I haven't figured out how to apply this to included relation entites yet but I hope it could help somebody looking for solution of this problem.

    I'm not sure why strapi does not support this since it is clearly capable of filtering the fields when they are explicitly set. it would be nice to use it like this:

    return Post
          .find()
          .fields(['title', 'content'])
          .sort(filters.sort)
          .skip(filters.start)
          .limit(filters.limit)
          .populate(populate);
    
    0 讨论(0)
  • 2021-01-18 07:06

    In the current strapi version (3.x, not sure about previous ones) this can be achieved using the select method in custom queries, regardless of which ORM is being used.

    SQL example:

     const restaurant = await strapi
        .query('restaurant')
        .model.query((qb) => {
            qb.where('id', 1);
            qb.select('name');
        })
        .fetch();
    
    0 讨论(0)
提交回复
热议问题