环回:关系通过 - 不起作用

Loopback: Relation Through - not working

所以,我遇到了一个问题,这个问题应该很简单,而且我确信我遗漏了一些明显的东西

我正在关注此文档:

所以我有 3 张桌子

客户、团队、客户-团队

client.json

{
  "name": "client",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "teams": {
      "type": "hasMany",
      "model": "team",
      "foreignKey": "teamId",
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}

team.json

{
  "name": "team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "type": {
      "type": "string",
      "required": true,
      "default": "first-team"
    },
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "clients": {
      "type": "hasMany",
      "model": "client",
      "foreignKey": "clientId",
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}

客户端-team.json

{
  "name": "client-team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "clientId": {
      "type": "string",
      "required": true
    },
    "teamId": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "client": {
      "type": "belongsTo",
      "model": "Client",
      "foreignKey": "clientId"
    },
    "team": {
      "type": "belongsTo",
      "model": "Team",
      "foreignKey": "teamId"
    }
  },
  "acls": [],
  "methods": {}
}

所以所有关系都设置正确(我认为)...

然后在我的客户中我有 1 个客户

[
  {
    "name": "Client name",
    "id": "59876185508eb519385779c6"
  }
]

在我的团队中有很多,但可以肯定的是:

[
  {
    "type": "type",
    "name": "Team name",
    "id": "5ae8a37add2989a32d37f83d"
  }
]

然后我去我的

localhost:3000/explorer

给POST一个客户团队

像这样

{
    "clientId": "59876185508eb519385779c6",
    "teamId": "5ae8a37add2989a32d37f83d"
}

我得到 200 响应:

{
  "clientId": "59876185508eb519385779c6",
  "teamId": "5ae8a37add2989a32d37f83d",
  "id": "5ae961873a7e3b33f0579fc3"
}

所以连接存在....

但是,当我去"GET client/id"做

id: 59876185508eb519385779c6 filter: {"include":["teams"]}

这是回应

{
  "name": "Chelsea FC",
  "id": "59876185508eb519385779c6",
  "teams": []
}

同样的情况发生在 "GET teams/id" 中,我使用

id: 5ae8a37add2989a32d37f83d filter: {"include":["clients"]}

或者如果我去 "GET teams/{id}/clients" 并放 编号:5ae8a37add2989a32d37f83d

我明白了

[]

那我做错了什么?我确定我错过了一件愚蠢的、显而易见的事情:/

使用 mongo 如果有任何区别

这里存在三个问题:

  1. 您将 mongodb 标识符描述为字符串,这就是您将字符串而不是对象 ID 存储在数据库中的原因。 (这不是必需的,因为数据源应该理解真实类型)
  2. 您的模型以小写字母开头。同应于关系也。 (问题的第一部分,它解决了 id 的问题)
  3. 客户和团队模型的关系不正确(问题的第二部分,修复包括)

客户端-team.json

{
  "name": "client-team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "clientId": {
      "type": "objectId", // !!! changed (not required)
      "required": true
    },
    "teamId": {
      "type": "objectId", // !!! changed (not required)
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "client": {
      "type": "belongsTo",
      "model": "client",  // !!! changed
      "foreignKey": "clientId"
    },
    "team": {
      "type": "belongsTo",
      "model": "team",  // !!! changed
      "foreignKey": "teamId"
    }
  },
  "acls": [],
  "methods": {}
}

client.json

{
  "name": "client",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "teams": {
      "type": "hasMany",
      "model": "team",
      "foreignKey": "clientId", // !!! changed (we describing id of this model, not team)
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}

team.json

{
  "name": "team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "type": {
      "type": "string",
      "required": true,
      "default": "first-team"
    },
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "clients": {
      "type": "hasMany",
      "model": "client",
      "foreignKey": "teamId", // !!! changed (the same as the previous)
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}