Loopback 2.4: how to request specific fields of a related model through a REST API

I have a user model above a relational database.

Each user can have "users", where "chiefId" is FK.

"relations": { "users": { "type": "hasMany", "model": "User", "foreignKey": "chiefId" }, } 

I can request the appropriate users for each main user as follows:

 GET /users?filter={"include":"users"} 

But it returns full user objects.

  • How can I request only the properties of the "name" of related users?
  • Can you also count related instances in a single server request?
+5
source share
3 answers

Late answer, but I now ran into this question. Maybe:

 filter: { include:{ relation: "users", scope: { fields:["name"] } } } 
+6
source

As far as I understand, this question concerns the addition of a nested filter at the inclusion level, which seems to be not yet supported: https://groups.google.com/forum/#!msg/loopbackjs/T6onsYMJFOI/V4ILc3Obf3MJ

Maybe this is not the best way to approach this problem, but what you can do is convert using a manual answer to .afterRemote ("find", ...).

+2
source
 /users?filter[fields][0]=name 

See https://github.com/strongloop/loopback-example-relations-basic for more details.

0
source

Source: https://habr.com/ru/post/983926/


All Articles