【问题标题】:Loopbackjs Querying related model via rest apiLoopbackjs 通过rest api查询相关模型
【发布时间】:2017-09-27 23:52:35
【问题描述】:

有什么方法可以查询相关模型的相关模型,比如从post\Get:

{"order": "created_at DESC","include":[{"relation": "user"}]

但是,在我的 user 模型中,hasonesettings 模型存在关系。我也想得到它,同时从post \Get rest api 查询。我试过了:

{ "include": { "relation": "user","include": {"relation":"settings"}}}

但没有运气。

【问题讨论】:

标签: node.js rest api loopbackjs loopback


【解决方案1】:

我已经创建了与您的问题相关的嵌套关系。

示例:teamRole.json

TeamRole > belongsTo > 用户和团队

  "validations": [],
  "relations": {
    "team": {
      "type": "belongsTo",
      "model": "Team",
      "foreignKey": ""
    },
    "user": {
      "type": "belongsTo",
      "model": "User",
      "foreignKey": ""
    }
  }

检索结果

app.models.TeamRole.findOne({
      where: {
        userId: user.id
      },
      include:[ {
        relation: 'team'
      },
{
        relation: 'user'
      } ]
    },function(err,team,user){
//retrieve relational data here
});

试试这个方法,希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多