Loopbackjs 通过 rest 查询相关模型 api

Loopbackjs Querying related model via rest api

有没有什么办法可以像post\Get:

那样查询related model的related model
{"order": "created_at DESC","include":[{"relation": "user"}]

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

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

但运气不好。

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

示例:teamRole.json

TeamRole > belongTo > 用户和团队

  "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
});

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