Loopback 无法通过 relation 获取 self 的数据

Loopback can't fetch data with self through relation

基于用户模型的会员模型

   {
      "name": "Member",
      "base": "User",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "nickname": {
          "type": "string"
        }
      },
      "validations": [],
      "relations": {
        "messages": {
          "type": "hasMany",
          "model": "Message",
          "foreignKey": ""
        },
        "followers": {
          "type": "hasMany",
          "model": "Member",
          "foreignKey": "followeeId",
          "through": "Follow"
        },
        "followings": {
          "type": "hasMany",
          "model": "Member",
          "foreignKey": "followerId",
          "through": "Follow"
        }
      },
      "acls": [
        {
          "accessType": "*",
          "principalType": "ROLE",
          "principalId": "$everyone",
          "permission": "DENY"
        },
        {
          "principalType": "ROLE",
          "principalId": "$owner",
          "permission": "ALLOW",
          "property": "__get__followers"
        },
        {
          "principalType": "ROLE",
          "principalId": "$owner",
          "permission": "ALLOW",
          "property": "__get__followings"
        },
        {
          "principalType": "ROLE",
          "principalId": "$owner",
          "permission": "ALLOW",
          "property": "__get__messages"
        }
      ],
      "methods": {}
    }

关注模特

{
  "name": "Follow",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "follower": {
      "type": "belongsTo",
      "model": "Member",
      "foreignKey": ""
    },
    "followee": {
      "type": "belongsTo",
      "model": "Member",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": {}
}

示例 1

使用数据库中的这些数据,当我尝试获取成员 1 的关注者和关注者时,我遇到了同样的错误。

Erreur non traitée pour la demande GET /api/Members/1/followers?access_token=t0oAVZM2CLJ7XLqQm2zxz8wj3fLmtUATHopDM40WYknLURbiObpVAlSD3DBEIOfv : Error: La relation "member" n'est pas définie pour le modèle Follow
        at processIncludeItem (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/include.js:289:10)
        at /home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/include.js:180:5
        at /home/thomas/Bureau/Projet/Project/node_modules/async/dist/async.js:3025:16
        at eachOfArrayLike (/home/thomas/Bureau/Projet/Project/node_modules/async/dist/async.js:940:9)
        at eachOf (/home/thomas/Bureau/Projet/Project/node_modules/async/dist/async.js:990:5)
        at Object.eachLimit (/home/thomas/Bureau/Projet/Project/node_modules/async/dist/async.js:3089:3)
        at Function.Inclusion.include (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/include.js:179:9)
        at /home/thomas/Bureau/Projet/Project/node_modules/loopback-connector-postgresql/node_modules/loopback-connector/lib/sql.js:1203:44
        at /home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:172:22
        at doNotify (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:99:49)
        at PostgreSQL.ObserverMixin._notifyBaseObservers (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:122:5)
        at PostgreSQL.ObserverMixin.notifyObserversOf (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:97:8)
        at cbForWork (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:162:14)
        at /home/thomas/Bureau/Projet/Project/node_modules/loopback-connector-postgresql/node_modules/loopback-connector/lib/sql.js:428:7
        at Query.<anonymous> (/home/thomas/Bureau/Projet/Project/node_modules/loopback-connector-postgresql/lib/postgresql.js:162:7)
        at Query.handleReadyForQuery (/home/thomas/Bureau/Projet/Project/node_modules/pg/lib/query.js:124:10)

示例 2

数据库中的这些数据是我尝试获取成员 1 的关注者时的结果。

我不明白为什么我无法在这两个示例中获取数据。顺便说一句,我正在使用 PostgreSQL。谢谢。

您需要根据 Member 修正 Follow 模型中的关系。

{
  "name": "Follow",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "follower": {
      "type": "belongsTo",
      "model": "Member",
      "foreignKey": "followeeId"
    },
    "followee": {
      "type": "belongsTo",
      "model": "Member",
      "foreignKey": "followerId"
    }
  },
  "acls": [],
  "methods": {}
}

并且在member.json

...
"followers": {
          "type": "hasMany",
          "model": "Member",
          "foreignKey": "followeeId",
          "keyThrough": "followerId",
          "through": "Follow"
        },
        "followings": {
          "type": "hasMany",
          "model": "Member",
          "foreignKey": "followerId",
          "keyThrough": "followeeId",
          "through": "Follow"
        }
...