IvanZh
IvanZh

Reputation: 2290

Loopback 2.4: how to query certain fields of related model via REST API

I have User model over relational DB.

Each User can hasMany "users" where "chiefId" is FK.

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

I can query related users for each chief-user like this:

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

But it returns full user objects.

Upvotes: 5

Views: 4030

Answers (3)

Hidde Stokvis
Hidde Stokvis

Reputation: 452

A late reply but I just ran into this question now. It is possible:

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

Upvotes: 8

superkhau
superkhau

Reputation: 2781

/users?filter[fields][0]=name

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

Upvotes: 0

Alex V
Alex V

Reputation: 1155

As far as I understood this question is about adding a nested filter on an include level, which seems to be not yet supported: https://groups.google.com/forum/#!msg/loopbackjs/T6onsYMJFOI/V4ILc3Obf3MJ

May be it's not the best way to approach this problem, but what you can do is a manual response transformation in .afterRemote('find', ...) hook.

Upvotes: 2

Related Questions