12

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://localhost:1337/api/reference?select=["name"]

Would ideally return something of the form

[{"name": "Ref1"}]

Unfortunately that is not the case, and in actuality it returns the following.

[
{
"contributors": [
{
"username": "aduensing",
"email": "standin@gmail.com",
"lang": "en_US",
"template": "default",
"id_ref": "1",
"provider": "local",
"id": 1,
"createdAt": "2016-07-28T19:39:09.349Z",
"updatedAt": "2016-07-28T19:39:09.360Z"
}
],
"createdBy": {
"username": "aduensing",
"email": "standin@gmail.com",
"lang": "en_US",
"template": "default",
"id_ref": "1",
"provider": "local",
"id": 1,
"createdAt": "2016-07-28T19:39:09.349Z",
"updatedAt": "2016-07-28T19:39:09.360Z"
},
"updatedBy": {
"username": "aduensing",
"email": "standin@gmail.com",
"lang": "en_US",
"template": "default",
"id_ref": "1",
"provider": "local",
"id": 1,
"createdAt": "2016-07-28T19:39:09.349Z",
"updatedAt": "2016-07-28T19:39:09.360Z"
},
"question": {
"createdBy": 1,
"createdAt": "2016-07-28T19:41:33.152Z",
"template": "default",
"lang": "en_US",
"name": "My Question",
"content": "Cool stuff, huh?",
"updatedBy": 1,
"updatedAt": "2016-07-28T19:45:02.893Z",
"id": "579a5ff83af4445c179bd8a9"
},
"createdAt": "2016-07-28T19:44:31.516Z",
"template": "default",
"lang": "en_US",
"name": "Ref1",
"link": "Google",
"priority": 1,
"updatedAt": "2016-07-28T19:45:02.952Z",
"id": "579a60ab5c8592c01f946cb5"
}
]

This immediately becomes problematic in any real world context if I decide to load 10, 20, 30, or more records at once, I and end up loading 50 times the data I needed. More bandwidth is used up, slower load times, etc.

Phil Pill
  • 303
  • 2
  • 14
Andrew Duensing
  • 195
  • 1
  • 3
  • 9

11 Answers11

8

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
idolgoff
  • 81
  • 1
  • 2
4

Here is how you can return specific fields and also exclude the relations to optimize the response.

  async list (ctx) {
    const result = await strapi.query('article').model.query(qb => {
          qb.select('id', 'title', 'link', 'content');
      }).fetchAll({
          withRelated: []
      }).catch(e => {
          console.error(e)
      });

    if(result) {
        ctx.send(result);
    } else {
        ctx.send({"statusCode": 404, "error": "Not Found", "message": "Not Found"});
    }
  }
Muhammad
  • 6,725
  • 5
  • 47
  • 54
3

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);
Droow
  • 416
  • 2
  • 10
  • 1
    not sure it this could help https://github.com/strapi/strapi/issues/2086#issuecomment-575936979 – Paolo Jan 25 '20 at 07:28
2

It would be better to have the query select the fields rather than relying on node to remove content. However, I have found this to be useful in some situations and thought I would share. The strapi sanitizeEntity function can include extra options, one of which allows you only include fields you need. Similar to what manually deleting the fields but a more reusable function to do so.

const { sanitizeEntity } = require('strapi-utils');

let entities = await strapi.query('posts').find({ parent: parent.id })

return entities.map(entity => {
  return sanitizeEntity(entity, {
    model: strapi.models['posts'],
    includeFields: ['id', 'name', 'title', 'type', 'parent', 'userType']
  });
});
2

It is now 2023, and for a little while it has been possible to do this using the fields parameter:

http://localhost:1337/api/reference?fields[0]=name&fields[1]=something
pusle
  • 1,393
  • 14
  • 18
1

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

Pierre
  • 986
  • 8
  • 13
0

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

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();
Gonzalo Faus
  • 464
  • 5
  • 13
0

not very beautiful,but you can delete it before return.

ref here: https://strapi.io/documentation/developer-docs/latest/guides/custom-data-response.html#apply-our-changes

const { sanitizeEntity } = require('strapi-utils');

module.exports = {
  async find(ctx) {
    let entities;
    if (ctx.query._q) {
      entities = await strapi.services.restaurant.search(ctx.query);
    } else {
      entities = await strapi.services.restaurant.find(ctx.query);
    }

    return entities.map(entity => {
      const restaurant = sanitizeEntity(entity, {
        model: strapi.models.restaurant,
      });
      if (restaurant.chef && restaurant.chef.email) {
        **delete restaurant.chef.email;**
      }
      return restaurant;
    });
  },
};
Lyman
  • 1
  • Please explain what your code does and how it does it. – M-Chen-3 Jan 29 '21 at 23:55
  • 1
    by default,strapi return all the fields to front, maybe contains some fields you dont want to return like the topic.so before you return your data,you can delete some fields of the object,and return the ones you want to using delete method. I think the reference I posted tell us all the info we need,please take a look. – Lyman Jan 30 '21 at 00:31
0

yeah,I remember another way. you can use the attribute in xx.settings.json file.

ref: model-options

{
  "options": {
    "timestamps": true,
    "privateAttributes": ["id", "created_at"],   <-this is fields you dont want to return
    "populateCreatorFields": true    <- this is the system fields,set false to not return
  }
}
Anuj Divkar
  • 252
  • 1
  • 10
Lyman
  • 1
0

You can override the default strapi entity response of:-

entity = await strapi.services.weeklyplans.create(add_plan);

return sanitizeEntity(entity, { model: strapi.models.weeklyplans });

By using:-

ctx.response.body = { 
              status: "your API status", 
              message: "Your own message" 
        }

Using ctx object, we can choose the fields we wanted to display as object. And no need to return anything. Place the ctx.response.body where the response has to be sent when the condition fulfilled.

sudhee_bsp
  • 58
  • 1
  • 9