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

家住魔仙堡 提交于 2019-12-07 04:03:53

问题


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.

  • How should I query only "name" properties of related users?
  • Also is it possible to count related instances in one request to server?

回答1:


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

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



回答2:


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.




回答3:


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

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



来源:https://stackoverflow.com/questions/26997368/loopback-2-4-how-to-query-certain-fields-of-related-model-via-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!