1

I would like to get a modified response object. For example I dont know how to get the user object without the roles.

The default response is:

{
    "id": 6,
    "username": "username",
    "email": "[email protected]",
    "provider": "local",
    "confirmed": true,
    "blocked": false,
    "role": {
        "id": 2,
        "name": "Authenticated",
        "description": "Default role given to authenticated user.",
        "type": "authenticated"
    }
}

Now I want to get the same response without the role attribute.

{
    "id": 6,
    "username": "username",
    "email": "[email protected]",
    "provider": "local",
    "confirmed": true,
    "blocked": false
}

2 Answers 2

1

Currently you cannot do this in the Rest API unless you change the UserController provided by permissions plugin, which is not recommended.

What you can do then is to use the GraphQL plugin provided by Strapi, so you can query only the fields you need on client side.

The docs about how to use GraphQL plugin are here.

Sign up to request clarification or add additional context in comments.

Comments

1

For anyone still struggling with this problem:

The latest versions of strapi do support custom queries, you can pass an array containing all the names of relations you wish to populate (only relations!).

If you don't want to populate any relationships, you can keep it empty, your controller would then look something like this:

module.exports = {    
UserWithoutRoles: ctx => {
      return strapi.query('user').findOne({ id: ctx.params.id }, ['']);
    }
}

If you do wish to populate it, it would be like this:

module.exports = {    
UserWithoutRoles: ctx => {
      return strapi.query('user').findOne({ id: ctx.params.id }, ['role']);
    }
}

Also see: [https://strapi.io/documentation/3.0.0-beta.x/concepts/queries.html#api-reference][1]

2 Comments

Is there any way of returning a custom object as reponse? for instance, instead of returning the sanitized object from a query, I want to return an object containing that response and other properties. If I do return {i: obj, j: obj} I get an unauthorized error
Access to new custom routes is restricted by default. Have you tried enabling permission to these routes? This can be either done programmatically (see strapi.io/documentation/3.0.0-beta.x/plugins/…) or in the admin panel under 'Roles and Permissions'

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.